嗨,我是MVC的新手已经做了几年的网络表格。
当用户访问我们的网站时,他们已经通过我们组织的中央身份验证服务进行了身份验证。我们已经拥有SQL Server数据库中的所有用户,只需要匹配他们的身份并分配适当的授权。我自定义了https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/AspNet.Identity.MySQL/Readme.txt以用于我们现有的SQL Server。 我收到“UserId not found”错误。 这是堆栈跟踪:
[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
以下是IdentityModel.cs中发生错误的地方:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
以下是我的AccountController.cs代码基于: ASP.NET MVC Identity login without password
var user = await UserManager.FindByNameAsync(id);
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: true, rememberBrowser: true);
}
答案 0 :(得分:0)
如果您有用户ID更改等待UserManager.FindByNameAsync(id) - &gt;等待UserManager.FindByIdAsync(Id)
FindByNameAsync - &gt;正在寻找匹配的用户名
答案 1 :(得分:0)
您好,您可以使用此方法UserManager.FindByIdAsync Method,它将按ID查找用户。
例如:
var user = await UserManager.FindByIdAsync(userid);
答案 2 :(得分:0)
我删除了
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);
我在How can I log a Client in Asp.Net MVC with no user找到了此代码。它似乎已经解决了我遇到的问题。