C# 将aspnet标识与现有MS SQL Server一起使用时,未找到用户ID错误

C# 将aspnet标识与现有MS SQL Server一起使用时,未找到用户ID错误,c#,sql-server,asp.net-mvc,authentication,asp.net-identity,C#,Sql Server,Asp.net Mvc,Authentication,Asp.net Identity,嗨,我是MVC的新手,已经做了几年web表单了 当用户到达我们的站点时,他们已经通过我们组织的中央身份验证服务进行身份验证。我们的SQL Server数据库中已经有所有用户,只需匹配他们的身份并分配适当的授权即可。我为使用现有的SQL Server进行了自定义。 我得到一个找不到的用户ID错误。 以下是堆栈跟踪: [InvalidOperationException: UserId not found.] Microsoft.AspNet.Identity.<GetSecurityS

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

当用户到达我们的站点时,他们已经通过我们组织的中央身份验证服务进行身份验证。我们的SQL Server数据库中已经有所有用户,只需匹配他们的身份并分配适当的授权即可。我为使用现有的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:\Security\Models\IdentityModels.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:\Security\Controllers\AccountController.cs:77

如果用户ID发生更改,请等待UserManager.FindByNameAsyncid->wait UserManager.FindByIdAsyncId


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

您好,您可以使用此方法,它将按ID查找用户

例:

我搬走了

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

我在上找到了这个代码。它似乎解决了我遇到的问题。

使用FindByIdAsync无法解决问题。我仍然收到相同的错误,找不到UserId。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.InvalidOperationException:UserId未找到。@可能UserId的值为null?我找不到UserId的位置。这篇文章似乎是相关的,但它似乎不适用于我。我的IdentityUser类中没有用户ID。使用FindByIdAsync无法解决此问题。我仍然收到相同的错误,找不到UserId。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.InvalidOperationException:找不到用户ID。
 var user = await UserManager.FindByIdAsync(userid);
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);