【发布时间】:2016-07-31 10:19:50
【问题描述】:
Visual Studio 2015 脚手架使用 UserManager<TUser>,它不能用于创建 ClaimsIdentity。有没有人有一个关于如何做到这一点的工作示例?
VS2015脚手架抛出错误:
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;
}
注意:我已向ApplicationUser 添加了与IdentyUser 不冲突的属性。
【问题讨论】:
-
脚手架会抛出什么错误,在哪里?您发布的方法显示它正在返回
ClaimsIdentity -
UserManager
不包含 CreateIdentityAsync 或 DefaultAuthenticationTypes 的定义 -
重现:VS2015 使用 MVC 6 模板创建一个新的 ASP.NET Web 项目。在 ApplicationUser.cs 中的模型下,添加对 System.Security.Claims 和 Micosoft.AspNet.Identity 的引用,并将上面的代码插入到 ApplicationUser 类中。请参阅上面评论中描述的错误。
-
您所描述的内容适用于 mvc 5 而不是 mvc 6/core。现在查看它在新版本中的变化
-
以后请使用正确的标签!当您的问题与 ASP.NET Core 相关时,请使用“asp.net-core”标签!不是“asp.net”和“core”,两者都与您的问题完全无关。添加前请阅读标签说明
标签: razor entity-framework-6 asp.net-core asp.net-identity-3