oriol.puig
oriol.puig

Reputation: 73

Trying to override CreateAsync function on ClaimsIdentityFactory

Few days ago, I found a very good tutorial about MVC, Identity and OWIN by Ben Foster. The tutorial is here

I've just finished the tutorial, and I've found a problem trying to override the function 'CreateAsync'. Visual Studio doesn't allow this.

This is the code:

public class AppUserClaimsIdentityFactory : ClaimsIdentityFactory<AppUser>
{
    public override async Task<ClaimsIdentity> CreateAsync(
        UserManager<AppUser> manager, 
        AppUser user, 
        string authenticationType)
    {
        var identity = await base.CreateAsync(manager, user, authenticationType);
        identity.AddClaim(new Claim(ClaimTypes.Country, user.Country));

        return identity;
    }
}

Anybody knows how can I resolve this issue?

Solution

Thanks to @ETorre, that's the final code!

public class AppUserClaimsIdentityFactory : ClaimsIdentityFactory<AppUser, string>
{
    public async override Task<ClaimsIdentity> CreateAsync(UserManager<AppUser, string> manager, 
            AppUser user, string authenticationType)
    {
        var identity = await base.CreateAsync(manager, user, authenticationType);
        identity.AddClaim(new Claim(ClaimTypes.Country, user.Country));

        return identity;
    }
}

Upvotes: 5

Views: 3824

Answers (2)

TechSavvySam
TechSavvySam

Reputation: 1442

I created a claims factory AppUserClaimsIdentityFactory similar to the one described above but wanted to add this clarification about how to use the factory. I added the following line to my AppUserManager constructor and it worked as needed:

public class AppUserManager : UserManager<AppUser, string>
{
    public AppUserManager(IUserStore<AppUser> userStore) : base(userStore)
    {
        ClaimsIdentityFactory = new AppUserClaimsIdentityFactory();
...

Upvotes: 1

ETorre
ETorre

Reputation: 134

You can try to see the version? the files on git use Microsoft.AspNet.Identity.Core.dll version 1 and now it's the vervion 2. Try to innerit of ClaimsIdentityFactory

Upvotes: 3

Related Questions