【发布时间】:2016-07-07 13:11:10
【问题描述】:
当我使用以下代码注册用户时:
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.UserName };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
return View(model);
}
}
我可以使用 [Authorize] 属性访问所有视图。 但是,如果我使用同一用户注销并重新登录,则会收到一个错误页面,其中包含
“InvalidOperationException:找不到用户 ID”
错误似乎来自我在声明中的 SignInAsync 函数:
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
我没有添加用户 ID,并且用户类(其 ID)或 AspNetUsers 表中没有用户 ID 字段。 该用户存在于该表中,但同样没有 UserId。
我已尝试清除 cookie 等,但仍然无法重新登录。 我是不是注册错了?我不明白为什么当 UserId 字段似乎不存在时,身份验证会寻找它
编辑: 当我第一次登录时...我得到错误页面。但是,当我重新打开页面并读取 cookie 时,我可以访问所有内容。初次登录到底发生了什么?
这里是 SignInAsync 方法:
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var _identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, _identity);
}
包含:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
【问题讨论】:
-
你能分享你的用户模型吗?
-
用户是一个IPrinicipal实现。我认为它已经融入其中。用户对象是一个 IdentityUser
-
你能告诉我们方法 SignInAsync(user, isPersistent: false); 的实现吗?
-
已添加。谢谢大家的帮助
-
@rigamonk - 你展示了两个不同的
CreateIdentityAsync。你指的是哪一个?您还应该展示您的ApplicationUser的外观,因为它可能与stackoverflow.com/a/35017046/5233410 有关。
标签: asp.net-mvc authentication