在Identity 3中创建声明标识

时间:2016-04-10 10:31:51

标签: razor entity-framework-6 asp.net-core asp.net-identity-3

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

N.B。:我已将ApplicationUser添加的属性与IdentyUser不冲突。

1 个答案:

答案 0 :(得分:0)

.net核心

根据herehere,答案已更改,两位作者都声明使用var stn_inp = document.getElementById("stn_inp"); var displayInp = document.getElementById("displayInput"); var nar = document.getElementById("narriator"); function showInp() { var inp = stn_inp.value; displayInp.innerHTML = inp; if (inp == "start game") { window.setTimeout(function () { nar.innerHTML = "Let's begin."; }, 500) window.setTimeout(function () { nar.innerHTML = "You are in the woods."; },1500) if (inp == "look around") { nar.innerHTML = "there is only trees"; } } } ,这是核心2.2的默认实现。第一篇文章说,您正在寻找的方法已经改变。但是,如上所述,您必须在这样的服务中注册UserClaimsPrincipalFactory<ApplicationUser>的实现,下面是一个示例类实现。请注意,我们必须注册UserClaimsPrincipalFactory,以便我们的服务集合知道在哪里可以找到它。这意味着在MyUserClaimsPrincipalFactory的构造函数中,它也引用了SignInManager<ApplicationUser>,但是服务会解决它:

IUserClaimsPrincipalFactory<ApplicationUser>

这是下面的课:

services
.AddIdentity<ApplicationUser, ApplicationRole>()              
.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>() // <======== HERE

services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();