忘记密码方法和编辑用户方法不起作用

Forgot Password method & Edit User method not working

我创建了 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(
................................................

好像在同一个地方有问题,但还没弄清楚

我明白了

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"));

你应该写信给 Startup.Auth class。谎言:

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;
        }

Important :You gotta be careful here

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

我遇到了同样的问题,经过多次研究我发现问题出在 IIS 部署中

所以跟随这个线程我能够解决我的问题

The data protection operation was unsuccessful

  1. Open your IIS Manager
  2. Find out what AppPool your application is using by selecting your App, right-click on it, and Select Manage Application -> Advanced Settings.
  3. After that, on the top left hand side, select Applications Pools,and go ahead and select the App Pool used by your app.
  4. Right-click on it, and select Advanced Settings, Go to the Process Model Section and Find the "Load User Profile" Option and set it to true.