自定义ASP.NET身份 [英] Customizing ASP.NET Identity

查看:60
本文介绍了自定义ASP.NET身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Identity Server 4,并且按如下方式自定义了我的ASP.NET Identity用户:

I'm using Identity Server 4 and I've customised my ASP.NET Identity user as follows:

public class ApplicationUser : IdentityUser
{
    [MaxLength(100)]
    public virtual string FirstName { get; set; }

    [MaxLength(100)]
    public virtual string LastName { get; set; }
}

我看不到在哪里将Identity Server 4配置为在Claims集合中包括这2个属性.我浏览了一些Identity Server 4示例,但看不到任何示例.

I can't see where I would configure Identity Server 4 to include these 2 properties in the claims collection. I've had a look through some of the Identity Server 4 samples but can't see any examples.

理想情况下,我希望将这两个用户属性映射到given_namefamily_name声明.

I'd ideally like to map these 2 user properties to the given_name and family_name claims.

我目前正在挂接到通知并查询userinfo端点(混合流?).因此,我不确定这是Identity Server的配置还是userinfo终结点的自定义?

I'm currently hooking up to the notifications and querying the userinfo endpoint (hybrid flow?). So I'm not sure if this is configuration of Identity Server or customization of the userinfo endpoint?

推荐答案

我想知道为什么没有关于此的文档.这使我意识到我可能做错了.

I was wondering why there is no documentation on this. It lead me to realise that I'm probably doing it wrong.

我没有看到作为ASP.NET Identity的一部分创建的表AspNetUserClaims.我将索赔数据添加到此处,并且索赔数据作为profile的一部分进入.

I'd not seen the table AspNetUserClaims created as part of ASP.NET Identity. I added my claim data into here and the claim data pulls through as part of the profile.

AccountController.RegisterPOST方法中,我添加了:

In the POST method for AccountController.Register I added:

var givenNameClaim = new IdentityUserClaim<string>()
{
    ClaimType = "given_name",
    ClaimValue = model.FirstName
};

var familyNameClaim = new IdentityUserClaim<string>()
{
    ClaimType = "family_name",
    ClaimValue = model.LastName
};

var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
user.Claims.Add(givenNameClaim);
user.Claims.Add(familyNameClaim);

这篇关于自定义ASP.NET身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆