-->

忘记密码的方法和编辑用户的方法不能正常工作(Forgot Password method & Edi

2019-10-23 12:00发布

我创建MVC 4应用程序。 在该应用程序

  1. 如果用户忘记了密码,我有方法来发送电子邮件到用户重置密码。

  2. 如果管理员想改变用户当前的密码,我有方法来发送电子邮件到用户的相关细节。

所以,我得到同样的错误,当我尝试发送电子邮件

我得到这样的错误以下

  1. 那我得到了忘记密码的方法错误

  1. 那我得到的编辑用户的方法错误

好像我在遇到麻烦时,我尝试发送电子邮件时,我使用asp.net成员身份

这是忘记密码方法相关的代码片段

            [HttpPost]
            [AllowAnonymous]
            [ValidateAntiForgeryToken]
            public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
            {

            if(ModelState.IsValid)
            {
                var username = await UserManager.FindByNameAsync(model.UserName);
                var user = await UserManager.FindByEmailAsync(model.Email);                   


                if (user != null && username != null)
                {

                        var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("My_Application");
                        UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));          
                        var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);                   


                        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
                        ........

这是编辑的用户方法相关的代码片段

    [HttpPost]
    [CustomAuthorization(IdentityRoles = "Admin")]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit_User(EditUserViewModel editUser)
    {    
        try
        {    
            if (ModelState.IsValid)
            {
                AspNetUser user = db.AspNetUsers.Find(editUser.Id);                                 

                if(editUser.Change == "Yes"){                    

                String userId = editUser.Id;
                String newPassword = editUser.NewPassword;

                var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("My_Application");
                UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
                var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
................................................

好像有在同一地点的问题,但无法弄清楚尚未

Answer 1:

我有同样的问题,那么许多研究后,我发现,问题出在IIS部署

所以下面这个线程我能解决我的问题

数据保护操作不成功

  1. 打开IIS管理器
  2. 找出你的应用程序是通过选择您的应用程序使用的是什么应用程序池,在其上单击鼠标右键,然后选择管理应用程序 - >高级设置。
  3. 在此之后,在顶部左侧,选择应用程序池,并继续并选择使用您的应用程序的应用程序池。
  4. 上单击鼠标右键,并选择高级设置,转到过程模型部分,然后找到“加载用户配置文件”选项并将其设置为true。


Answer 2:

我懂了

if (user != null && username != null)

你想设置那些在构造函数? 如果是这样,你不能,你需要设置它们的方法。



Answer 3:

您会收到你写来错了地方的错误代码。

var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("My_Application");
UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));

您可以写信给Startup.Auth类。 躺在这样的:

app.CreatePerOwinContext(IdentityFactory.CreateContext);
app.CreatePerOwinContext<CustomUserManager>(IdentityFactory.CreateUserManager);

用户管理器定义和设置

 public static CustomUserManager CreateUserManager(IdentityFactoryOptions<CustomUserManager> options, IOwinContext context)
        {
            var manager = new CustomUserManager(new CustomUserStore(context.Get<CustomIdentityDbContext>()));

            manager.UserValidator = new UserValidator<CustomUser, int>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(10);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

            manager.EmailService = new IdentityEmailService();

            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = new DataProtectorTokenProvider<CustomUser, int>(dataProtectionProvider.Create("My_Application"))
                {
                    TokenLifespan = TimeSpan.FromHours(2)
                };
            }

            return manager;
        }

重要提示 :你必须要小心这里

 var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = new DataProtectorTokenProvider<CustomUser, int>(dataProtectionProvider.Create("FocusOnStoreService"))
                {
                    TokenLifespan = TimeSpan.FromHours(2)
                };
            }


文章来源: Forgot Password method & Edit User method not working