将 aspnet Identity 与现有 MS SQL Server 一起使用,找不到用户 ID 错误



嗨,我是MVC的新手,已经做了几年的网络表单。

当用户访问我们的网站时,他们已经通过我们组织的中央身份验证服务进行身份验证。 我们的 SQL Server 数据库中已经有所有用户,只需要匹配他们的身份并分配适当的权限。 我自定义了 https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/AspNet.Identity.MySQL/Readme.txt 以用于我们现有的SQL Server。 我收到"找不到用户 ID"错误。 下面是堆栈跟踪:

[InvalidOperationException: UserId not found.]
Microsoft.AspNet.Identity.<GetSecurityStampAsync>d__42.MoveNext() +651
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +49
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +109
Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +1350
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +49
Security.Models.<GenerateUserIdentityAsync>d__0.MoveNext() in T:SecurityModelsIdentityModels.cs:16
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
Microsoft.AspNet.Identity.Owin.<SignInAsync>d__2.MoveNext() +437
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
Security.Controllers.<Login>d__10.MoveNext() in T:SecurityControllersAccountController.cs:77

以下是 IdentityModel 中发生错误的地方.cs:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

下面是我的帐户控制器.cs基于的代码: ASP.NET MVC身份登录无需密码

var user = await UserManager.FindByNameAsync(id);
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: true, rememberBrowser: true);
}

我删除了

var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

并将其替换为

var userIdentity = new ClaimsIdentity(new[]
{
// adding following 2 claims for supporting default antiforgery provider
new Claim(ClaimTypes.NameIdentifier, this.UserName),
new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
new Claim(ClaimTypes.Name, this.UserName),
new Claim("AspNet.Identity.SecurityStamp", this.SecurityStamp),
//new Claim(ClaimTypes.Role, AppRoleClaims.Client),
new Claim(ClaimTypes.Sid, this.Id.ToString())
},
DefaultAuthenticationTypes.ApplicationCookie);

我在如何在没有用户的情况下 Asp.Net MVC 中登录客户端中找到了此代码。它似乎已经解决了我遇到的问题。

如果您有 userid 更改,请等待 UserManager.FindByNameAsync(id( ->await UserManager.FindByIdAsync(Id(

FindByNameAsync -> 正在查找匹配的用户名

嗨,您可以使用此方法UserManager.FindByIdAsync Method,它将通过ID查找用户。

前任:

var user = await UserManager.FindByIdAsync(userid);

最新更新