private MembershipUser UserToMembershipUser(User user)
 {
     return new RavenDBMembershipUser(_providerName, user.Username, user.Id, user.Email, null, null, true, false
         , user.DateCreated, user.DateLastLogin.HasValue ? user.DateLastLogin.Value : new DateTime(1900, 1, 1), new DateTime(1900, 1, 1), new DateTime(1900, 1, 1), new DateTime(1900, 1, 1));
 }
Exemplo n.º 2
0
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (_membershipService.ValidateUser(model.UserName, model.Password))
                {
                    _formsService.SignIn(model.UserName, model.RememberMe);

                    for (int i = 0; i < 5; i++)
                    {
                        User u = new User();
                        u.Username = model.UserName + " " + i.ToString();
                        u.DateLastLogin = DateTime.UtcNow;
                        u.DateCreated = DateTime.UtcNow;
                        u.Roles = new List<string>();

                        UserLoginEvent loginEvent = new UserLoginEvent();
                        loginEvent.CreationDate = DateTime.Now;
                        loginEvent.User = u;

                        BaseController.ServiceBus.PublishAsync<UserLoginEvent>(loginEvent, (result) => { });
                    }

                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public override MembershipUser CheckedCreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            if (password.Length < MinRequiredPasswordLength)
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);

            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);
            OnValidatingPassword(args);
            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            var user = new User();
            user.Username = username;
            password = password.Trim();
            user.PasswordSalt = PasswordUtil.CreateRandomSalt();
            user.PasswordHash = PasswordUtil.HashPassword(password, user.PasswordSalt);
            user.Email = email;
            user.ApplicationName = this.ApplicationName;
            user.DateCreated = DateTime.Now;

            using (var session = this.DocumentStore.OpenSession())
            {
                session.Advanced.UseOptimisticConcurrency = true;

                try
                {
                    session.Store(user);
                    session.Store(new ReservationForUniqueFieldValue { Id = "username/" + user.Username });
                    session.Store(new ReservationForUniqueFieldValue { Id = "email/" + user.Email });

                    session.SaveChanges();

                    status = MembershipCreateStatus.Success;

                    return new MembershipUser(_providerName, username, user.Id, email, null, null, true, false, user.DateCreated,
                        new DateTime(1900, 1, 1), new DateTime(1900, 1, 1), DateTime.Now, new DateTime(1900, 1, 1));
                }
                catch (ConcurrencyException e)
                {
                    status = InterpretConcurrencyException(user.Username, user.Email, e);
                }
                catch (Exception ex)
                {
                    // TODO: log exception properly
                    Console.WriteLine(ex.ToString());
                    status = MembershipCreateStatus.ProviderError;
                }
            }
            return null;
        }