Extend ClaimsPrincipal and ClaimsIdentity classes in MVC-6

I've been trying to figure this out for a few days, but I have to ask you guys (again).

As the title says, I would like to implement my custom ClaimsPrincipal and ClaimsIdentity so that I can add some more properties to my Identity instance.

I did this earlier in MVC-5 using Global.asax.cs and an inherited BaseController. In MVC-6 it seems that startup.cs will be the entry point for this, but I cannot figure it out.

These are my two classes:

public class BaseIdentity : ClaimsIdentity
{
    public Guid UserId { get; set; }
    public Guid OrgId { get; set; }
    public string OrgName { get; set; }

    public BaseIdentity()
    {
    }
}

      

AND..

public class BasePrincipal : ClaimsPrincipal
{
    private readonly BaseIdentity _identity;

    public BasePrincipal(BaseIdentity identity)
    {
        _identity = identity;
    }

    public new BaseIdentity Identity
    {
        get { return _identity; }
    }
}

      

How can I use these classes instead of the default when creating / retrieving Auth cookies?

There is a method called IApplicationBuilder.UseClaimsTransformation()

but can't find any documentation on how to use it - if it's even for this scenario?

Help rate at this moment! :)

BTW: I would like to point out that I asked a similar question a week ago when I first encountered this problem. I got a response, but this is what I really need to fix in order for my site to be usable on MVC-6.

+3


source to share


1 answer


I did this using extension methods instead of inheritance.



public static class PrincipalExtensions
{
    public static string GetEmployeeNumber(this ClaimsPrincipal claimsPrincipal)
    {
        return claimsPrincipal.FindFirstValue("EmployeeNumber");
    }
}

      

+4


source







All Articles