0

I want to use AspNetCore.Identity for my backend. I am using MongoDb, so I am creating a custom UserStore implementation. It is my first time setting this up and I am a little uncertain about the following aspects:

  1. Claims without roles: Can I skip everything about roles (e.g., no RoleManager, no implementation of Role related interfaces in the UserStore)?
  2. Registering: Am I missing any interfaces which I would need to add to my IdentityBuilder? I did not find clear explanation which aspects I need for my case as a lot of examples were either too simplistic or extremely complex.
  3. Claims for auth: Am I setting up the usage of claims for authorization correctly?

This is how I plan setup all identity related aspects:

    public static IdentityBuilder AddIdentityStore
    (this IServiceCollection services, Action<IdentityOptions> setupIdentityAction)
    {
        var builder = services.AddIdentityCore<MyUser>()
            .AddUserStore<MyUserStore>()
            .AddUserManager<UserManager<MyUser>>()
            .AddDefaultTokenProviders();

        var sp = services.BuildServiceProvider();
        var db = sp.GetService<IDatabase>();

        if (db == null)
        {
            throw new ArgumentNullException(nameof(db), "Database not available as service");
        }

        var userCollection = db.GetCollection<MyUser>(nameof(MyUser));
        services.AddSingleton(s => userCollection);
        services.AddTransient<IUserStore<MyUser>>(s => new MyUserStore(userCollection));

        return builder;
    }

And this is how I then plan to use the claims on the user for authentication

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("HasAbc", policy => policy.RequireClaim("Abc"));
});

Thanks for your help!

1
  • Per my understanding, you are tring to added asp.net core identity into your app but you want to use MongoDb instead of the a default sql server db. I find a blog here which might be helpful to you. Commented Feb 7 at 3:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.