尝试执行SignInAsync时出现InvalidOperationException

Lut*_*ndo 6 asp.net-core-mvc asp.net-core asp.net-core-identity

我正在尝试在普通控制器中签署用户.我有类似的东西:

var claims = new List<Claim>
{
    new Claim(JwtClaimTypes.Subject, "xxx-xxx-xx-xxx),
    new Claim(JwtClaimTypes.PreferredUserName, "someusername"),
    new Claim(JwtClaimTypes.Email, "foo@bar"),
    new Claim(JwtClaimTypes.EmailVerified, "foo@bar:),
    new Claim(JwtClaimTypes.IdentityProvider, "idsvr"),
};

var ci = new ClaimsIdentity(claims, "password", JwtClaimTypes.PreferredUserName, JwtClaimTypes.Role);
var cp = new ClaimsPrincipal(ci);
await HttpContext.Authentication.SignInAsync("myscheme", cp);
Run Code Online (Sandbox Code Playgroud)

我得到一个例外说法 InvalidOperationException: name claim is missing

在ClaimsIdentity的构造函数中,我指定了我的名字声称是JwtClaimTypes.PreferredUsername(这是"preferred_username")

如果我确实在声明列表中添加了名称声明,则登录可以正常使用上面的代码段.但是,我们实际上在我们的系统中没有名称声明.我们希望在抽象中使用"preferred_username"作为名称声明.