コード例 #1
0
 public async Task<IdentityResult> ChangePassword(MyUser theUser, ChangePasswordModel model)
 {
     var db = new DataContext();
     var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
     userMan.UserValidator = new UserValidator<MyUser>(userMan)
     {
         AllowOnlyAlphanumericUserNames =
             false
     };
     //var theUser = await UserManager.FindByNameAsync(userName);
     var result = await userMan.ChangePasswordAsync(theUser.Id, model.OldPassword, model.NewPassword);
     return result;
 }
コード例 #2
0
        public async Task<JsonData> Register(UserViewModel model)
        {
            try
            {
                using (var db = new DataContext())
                {
                    var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
                    userMan.UserValidator = new UserValidator<MyUser>(userMan)
                    {
                        AllowOnlyAlphanumericUserNames =
                            false
                    };

                    var user = new MyUser
                    {
                        UserName = model.UserName.Trim(),
                        FullName = model.FullName,
                        IsActive = model.IsActive,
                        PhoneNumber = model.PhoneNumber,
                        Email = model.Email,
                        DateOfBirth = model.DateOfBirth,
                        Created = DateTime.Now,
                        Updated = DateTime.Now
                    };

                    var result = await userMan.CreateAsync(user, model.Password.Trim());

                    if (!result.Succeeded)
                    {
                        var msg = string.Join(", ", result.Errors);
                        msg = msg.Replace("Name", "User Name");
                        msg = msg.Replace("is already taken.", "is aready in use.");
                        throw new Exception(msg);
                    }

                    userMan.AddToRole(user.Id, model.Roles);

                    db.SaveChanges();
                    return DataHelpers.ReturnJsonData(user.Id, true, "Registration was successful. Please Verify your account");
                }
            }
            catch (Exception e)
            {
                return DataHelpers.ExceptionProcessor(e);
            }
        }