Asp.net 身份 2 User.Identity.GetUserId<int>() 总是 returns 0?
Asp.net Identity 2 User.Identity.GetUserId<int>() always returns 0?
我已经扩展了 Asp.net Identity 2 模型,通过遵循 post 之类的 this and this 来使用整数键。但是,这行代码总是returns0.
User.Identity.GetUserId<int>()
即使 User.Identity.IsAuthenticated 为真并且 User.Identity.GetUserName() returns 正确的用户名。我已经看到 this post 但它没有帮助,因为我已经在控制器方法而不是构造函数中调用 User.Identity.GetUserId() 。 post 中对 "getUserIdCallback" 的引用很有趣,也许我需要类似的东西。任何帮助深表感谢。
事实证明,我需要在自定义 OAuthAuthorizationServerProvider 的 GrantResourceOwnerCredentials 方法中将用户 ID 添加到 ClaimsIdentity。方法如下:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin") ?? "*";
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
//THIS IS THE IMPORTANT LINE HERE!!!!!
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim("sub", context.UserName));
foreach (var role in userManager.GetRoles(user.Id))
{
identity.AddClaim(new Claim(ClaimTypes.Role, role));
}
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{ "as:client_id", context.ClientId ?? string.Empty }
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
}
我已经扩展了 Asp.net Identity 2 模型,通过遵循 post 之类的 this and this 来使用整数键。但是,这行代码总是returns0.
User.Identity.GetUserId<int>()
即使 User.Identity.IsAuthenticated 为真并且 User.Identity.GetUserName() returns 正确的用户名。我已经看到 this post 但它没有帮助,因为我已经在控制器方法而不是构造函数中调用 User.Identity.GetUserId() 。 post 中对 "getUserIdCallback" 的引用很有趣,也许我需要类似的东西。任何帮助深表感谢。
事实证明,我需要在自定义 OAuthAuthorizationServerProvider 的 GrantResourceOwnerCredentials 方法中将用户 ID 添加到 ClaimsIdentity。方法如下:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin") ?? "*";
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
//THIS IS THE IMPORTANT LINE HERE!!!!!
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim("sub", context.UserName));
foreach (var role in userManager.GetRoles(user.Id))
{
identity.AddClaim(new Claim(ClaimTypes.Role, role));
}
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{ "as:client_id", context.ClientId ?? string.Empty }
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
}