当前位置: 首页>>代码示例>>C#>>正文


C# ApplicationUserManager.ResetPasswordAsync方法代码示例

本文整理汇总了C#中ApplicationUserManager.ResetPasswordAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.ResetPasswordAsync方法的具体用法?C# ApplicationUserManager.ResetPasswordAsync怎么用?C# ApplicationUserManager.ResetPasswordAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ApplicationUserManager的用法示例。


在下文中一共展示了ApplicationUserManager.ResetPasswordAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetUserPassword

        public async Task<bool> SetUserPassword(int userId, string password)
        {
            logger.Info("SetUserPassword");
            if (userId == 0)
            {
                throw new KMBitException("用户编号不能为0");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new KMBitException("密码不能为空");
            }           
            ApplicationUserManager manager = null;
            BUser requestedUser = GetUserInfo(userId);
            if(!requestedUser.IsAdmin)
            {
                if (!CurrentLoginUser.Permission.UPDATE_USER_PASSWORD)
                {
                    throw new KMBitException("没有权限设置用户密码");
                }
            }else if(requestedUser.IsSuperAdmin)
            {
                if (!CurrentLoginUser.IsWebMaster)
                {
                    throw new KMBitException("只有站长可以设置超级管理员密码");
                }
            }else if(requestedUser.IsWebMaster)
            {
                throw new KMBitException("任何人都没有权限修改站长密码");
            }
            bool ret = false;
            try
            {
                var provider = DataProtectionProvider != null ? DataProtectionProvider : new DpapiDataProtectionProvider("Sample");
                logger.Info("provider is created");
                manager = new ApplicationUserManager(new ApplicationUserStore(new chargebitEntities()));
                manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, int>(provider.Create("usertoken"));
                logger.Info("DataProtectorTokenProvider is created");
                string code = manager.GeneratePasswordResetToken(userId);
                logger.Info("code:" + code);
                //logger.Info("newpassword:" + password);
                var result = await manager.ResetPasswordAsync(userId, code, password);
                if (result.Succeeded)
                {
                    logger.Info("SetUserPassword succeed");
                    ret = true;
                }
                else
                {
                    logger.Info("SetUserPassword failure");
                    ret = false;
                }
            }
            catch (DbEntityValidationException dbex)
            {
                var errorMessages = dbex.EntityValidationErrors
                     .SelectMany(x => x.ValidationErrors)
                     .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(dbex.Message, " The validation errors are: ", fullErrorMessage);
                logger.Error(exceptionMessage);
            }
            catch (Exception ex)
            {
                logger.Fatal(ex);
            }
            finally
            {
                if (manager != null)
                {
                    manager.Dispose();
                }
            }
            logger.Info("Leaving SetUserPassword");
            return ret;
        }
开发者ID:Bobom,项目名称:kuanmai,代码行数:79,代码来源:UserManagement.cs

示例2: InsertOrUpdate

        public async Task<bool> InsertOrUpdate(IdentityUserViewModel model, ApplicationUserManager userManager)
        {

            var user = await userManager.FindByIdAsync(model.Id);

            if (user == null)
            {
                user = new ApplicationUser();
                user.Assign(model);


                var result = await userManager.CreateAsync(user, "1234567");
                if (!result.Succeeded) return false;
                model.Id = user.Id;
            }
            else
            {
                user.Email = model.Email;
                user.UserName = model.UserName;
                user.DUI = model.DUI;
                user.PhoneNumber = model.PHONE_2;
                user.ADDRESS = model.ADDRESS;
                user.Category = model.Category;
                user.FirstName = model.FirstName;
                user.LastName = model.LastName;
                user.DUI = model.DUI;
                user.PHONE_2 = model.PHONE_2;
                user.ProfilePicture = model.ProfilePicture;
                user.CenterId = model.CenterId;
                //user.Address = model.Address;
                //user.FirstName = model.FirstName;
                //user.LastName = model.LastName;
                //user.DocumentNum = model.DocumentNum;
                //user.ProfilePicture = model.ProfilePicture;
                await userManager.UpdateAsync(user);
            }

            if (model.ForceChangePassword)
            {
                var tok = await userManager.GeneratePasswordResetTokenAsync(model.Id);

                var result = await userManager.ResetPasswordAsync(model.Id, tok, model.Password);

                if (!result.Succeeded) return false;
            }
            var roles = await userManager.GetRolesAsync(model.Id);

            if (!roles.Any() && !string.IsNullOrEmpty(model.Role))
            {
                var res = await userManager.AddToRoleAsync(model.Id, model.Role);
            }

            if (roles.All(r => r != model.Role) && roles.Any())
            {
                var result = await userManager.AddToRoleAsync(model.Id, model.Role);
            }
            roles.Where(r => r != model.Role).ToList().ForEach(role => userManager.RemoveFromRole(model.Id, role));

            if (model.CenterId != 0)
            {

                var claim = await Context.UserClaims.FirstOrDefaultAsync(c => c.UserId == user.Id && c.ClaimType == "CenterId");

                var claims = await userManager.GetClaimsAsync(user.Id);
                if (claim != null)
                {
                    claim.ClaimValue = model.CenterId.Value.ToString();
                    await Context.SaveChangesAsync();
                }
                else
                {
                    await userManager.AddClaimAsync(model.Id, new System.Security.Claims.Claim("CenterId", model.CenterId.ToString()));
                }
            }


            return true;
        }
开发者ID:QualityConsultingTeam,项目名称:Cancha,代码行数:78,代码来源:IdentityManagerService.cs


注:本文中的ApplicationUserManager.ResetPasswordAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。