且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MVC5:UserManager.AddToRole():“将用户添加到角色时出错:未找到 UserId"?

更新时间:2023-02-25 20:58:08

我把这个留在这里给可能有类似问题的任何人.我有完全相同的症状".事实证明,与存储的密码相关的问题不符合配置的密码策略(至少一个大写字符、一个小写字符等).

I am leaving this here for anyone that might have had a similar issue. I had exactly the same "symptoms". It turns out that the problem related to the password being stored not adhering to the configured password policy (at least one uppercase char, one lowercase char, etc etc).

根据以下评论和答案,通常,当不遵循任何用户创建约束或政策时,会抛出相同的模糊错误消息.

As per below comments and answers, in general, the same vague error message is thrown when any of the user creation constraints or policies are not followed.

这可能包括以下内容:

  • 未遵循密码政策(这似乎是最常见的原因)
  • 必填字段作为空字符串/null 传递
  • 重复的用户名或电子邮件

确实有很多问题会导致发生此错误.如果以上方法不能解决您的问题,我的建议如下:

There is really a wide range of issues which can cause this error to occur. If the above does not solve your problem, I suggest the following:

  1. 熟悉为您的解决方案设置的身份提供商的规则和政策
  2. 按照下面@Ted 的回答,在 UserManager.Create 方法上跟踪或放置断点以查看结果,因为这可能会揭示问题的原因.