简体   繁体   English

重置Microsoft Identity上的密码会导致System.Security.Cryptography.CryptographicException

[英]Resetting password on Microsoft Identity causes System.Security.Cryptography.CryptographicException

I'm using MS Identity and when I'm using a reset token to reset the password, I get this exception: 我正在使用MS Identity ,当我使用重置令牌重置密码时,我得到以下异常:

Inner Exception Type: System.Security.Cryptography.CryptographicException Inner Exception: The data protection operation was unsuccessful. 内部异常类型:System.Security.Cryptography.CryptographicException内部异常:数据保护操作不成功。 This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating. 这可能是由于没有为当前线程的用户上下文加载用户配置文件引起的,这可能是线程模拟时的情况。

This is what the code looks like: 这就是代码的样子:

var TheProvider = new DpapiDataProtectionProvider();
UserManager<IdentityUser> TheUserManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
TheUserManager.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser>(TheProvider.Create("EmailConfirmation"));

string TheResetCode = TheUserManager.GeneratePasswordResetToken(TheUserMembershiptID);

IdentityResult TheResult = TheUserManager.ResetPassword(TheUserMembershiptID, TheResetCode, TheNewPassword);

What I have works on my local machine but not when I put it on a server. 我在本地机器上工作的是什么,但是当我把它放在服务器上时却没有。 I've looked around but I haven't found a solution. 我环顾四周,但我还没有找到解决方案。 What do I need to change to make it work? 我需要更改什么才能使其正常工作?

This is a configuration problem with IIS. 这是IIS的配置问题。 Go to the application pool advanced settings, and set the option "Load User Profile" to True . 转到应用程序池高级设置,并将“加载用户配置文件”选项设置为True

The assumption here is that you are using at least version 3 of Identity along with OWIN and entity framework. 这里的假设是您至少使用Identity 3的版本以及OWIN和实体框架。

Don't create the data protection provider manually every time. 不要每次都手动创建数据保护提供程序。 Get it at startup from the application builder and store it for the user manager to use. 从应用程序构建器启动时获取它并将其存储以供用户管理器使用。

public static class Auth {
    internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
}

public partial class Startup {
    public void ConfigureAuth(IAppBuilder app) {
        Auth.DataProtectionProvider = app.GetDataProtectionProvider();
        //...other code removed for brevity
    }
}

Configure a DbContext for membership information persistence 为成员资格信息持久性配置DbContext

public class MyIdentityDbContext : IdentityDbContext<IdentityUser> {
    public MyIdentityDbContext()
        : base("MembershipConnection") { }

    public static MyIdentityDbContext Create() {
        return new MyIdentityDbContext();
    }
}

Now create a UserManager derived class and configure it to use the data protection provider 现在创建一个UserManager派生类并将其配置为使用数据保护提供程序

public class IdentityUserManager : UserManager<IdentityUser> {

    private IdentityUserManager()
        : base(new UserStore<IdentityUser>(MyIdentityDbContext.Create())) {
        //...other code removed for brevity

        var dataProtectionProvider = Auth.DataProtectionProvider;
        if (dataProtectionProvider != null) {
            this.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser>(dataProtectionProvider.Create("UserToken"));
        }
    }

    public static IdentityUserManager Create() {
        return new IdentityUserManager();
    }
}

So now assuming you have users registered in your persistence storage, you should be able to generate your token and reset the password. 因此,现在假设您在持久性存储中注册了用户,您应该能够生成令牌并重置密码。

var userManager = IdentityUserManager.Create();

var resetToken = await userManager.GeneratePasswordResetTokenAsync(id);

var result = await userManager.ResetPasswordAsync(id, resetToken, newPassword);

Now based you your comments in the post, It could very well be that the Load User Profile in the host (assuming IIS) needs to be set to true . 现在根据你在帖子中的评论,很可能是主机中的Load User Profile (假设IIS)需要设置为true

Quoting this answer 引用这个答案

I had the same issues except i was hosting on amazon ec2. 我有同样的问题,除了我在亚马逊ec2主办。 i was able to resolve it by going to the application pool in IIS and (under advanced settings after a right click) setting process model - load user profile = true. 我能够通过转到IIS中的应用程序池和(在右键单击后的高级设置下)设置流程模型来解决它 - 加载用户配置文件= true。

If that is the case and you don't have access to the server to be able to change that setting like you indicated in the comments, then there is not much else that the community can provide that has not already been covered in posts that encountered this particular issue. 如果是这种情况,并且您无法访问服务器以便能够像您在评论中指示的那样更改该设置,那么社区可以提供的其他内容并没有在遇到的帖子中提及这个特殊问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给出错误密码时出现System.Security.Cryptography.CryptographicException - System.Security.Cryptography.CryptographicException when wrong password given TwilioRequestValidator 中的瞬态 System.Security.Cryptography.CryptographicException - Transient System.Security.Cryptography.CryptographicException in TwilioRequestValidator System.Security.Cryptography.CryptographicException:句柄无效 - System.Security.Cryptography.CryptographicException: The handle is invalid System.Security.Cryptography.CryptographicException:参数不正确 - System.Security.Cryptography.CryptographicException: The parameter is incorrect System.Security.Cryptography.CryptographicException:'Cryptography_OAEPDecoding' - System.Security.Cryptography.CryptographicException: 'Cryptography_OAEPDecoding' c#,对密码登录进行加密和解密。 System.Security.Cryptography.CryptographicException:错误的数据。 错误 - c# , encrypt and Decrypt for the password login. System.Security.Cryptography.CryptographicException: Bad Data. error System.Security.Cryptography.CryptographicException:系统找不到指定的文件 - System.Security.Cryptography.CryptographicException: The system cannot find the file specified System.Security.Cryptography.CryptographicException:密钥集不存在 - System.Security.Cryptography.CryptographicException: keyset does not exist PrivateKey抛出了System.Security.Cryptography.CryptographicException类型的异常 - PrivateKey threw an exception of type System.Security.Cryptography.CryptographicException System.Security.Cryptography.CryptographicException:'输入数据不是一个完整的块。' - System.Security.Cryptography.CryptographicException: 'The input data is not a complete block.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM