Adding custom claim not working in asp.net core identity(添加自定义声明在 asp.net 核心身份中不起作用)
问题描述
我创建了 CustomClaimType
来存储 user id
:
I have created CustomClaimType
to store user id
:
public static class CustomClaimTypes
{
public const string UserId = "UserId";
}
当用户登录时,我设置它:
When user login, I set it:
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, doc_session.ufname + " " + doc_session.ulname));
claims.Add(new Claim(CustomClaimTypes.UserId, doc_session.isci_id.Value.ToString()));
ClaimsIdentity userIdentity = new ClaimsIdentity(claims,"Identity.Application");
ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
我还创建了自定义身份扩展:
I have also created custom identity extension:
public static class IdentityExtensions
{
public static int GetUserId(this IIdentity identity)
{
ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
Claim claim = claimsIdentity?.FindFirst(CustomClaimTypes.UserId);
if (claim == null)
return 0;
return int.Parse(claim.Value);
}
public static string GetName(this IIdentity identity)
{
ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
Claim claim = claimsIdentity?.FindFirst(ClaimTypes.Name);
return claim?.Value ?? string.Empty;
}
}
但是当我调用 User.Identity.GetUserId();
我得到 0 (null) 结果.
But when I call User.Identity.GetUserId();
I get 0 (null) as result.
推荐答案
要向 Identity 添加额外的声明,您可以实现自定义 CustomClaimsPrincipalFactory
.
For adding extra claims to Identity, you could implement custom CustomClaimsPrincipalFactory
.
CustomClaimsPrincipalFactory
public class CustomClaimsPrincipalFactory : UserClaimsPrincipalFactory<IdentityUser<int>>
{
public CustomClaimsPrincipalFactory(UserManager<IdentityUser<int>> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
}
public async override Task<ClaimsPrincipal> CreateAsync(IdentityUser<int> user)
{
var principal = await base.CreateAsync(user);
// Add your claims here
((ClaimsIdentity)principal.Identity).AddClaims(
new[] { new Claim(ClaimTypes.Name, user.UserName),
new Claim(CustomClaimTypes.UserId, user.Id.ToString())
});
return principal;
}
}
注册CustomClaimsPrincipalFactory
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser<int>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddScoped<IUserClaimsPrincipalFactory<IdentityUser<int>>, CustomClaimsPrincipalFactory>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
IdentityExtensions
public static class IdentityExtensions
{
public static int GetUserId(this ClaimsPrincipal identity)
{
Claim claim = identity?.FindFirst(CustomClaimTypes.UserId);
if (claim == null)
return 0;
return int.Parse(claim.Value);
}
public static string GetName(this ClaimsPrincipal identity)
{
Claim claim = identity?.FindFirst(ClaimTypes.Name);
return claim?.Value ?? string.Empty;
}
}
用途
Useage
public IActionResult About()
{
var claims = User.Claims;
var userId = User.GetUserId();
var userName = User.GetName();
ViewData["Message"] = "Your application description page.";
return View();
}
这篇关于添加自定义声明在 asp.net 核心身份中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:添加自定义声明在 asp.net 核心身份中不起作用
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01