Identity 2.0 with custom tables

前端 未结 3 488
予麋鹿
予麋鹿 2021-01-30 08:59

I\'m new to ASP.NET identity and am still trying to get my head around how it all works. Unfortunately I\'ve found many of the tutorials I\'ve tried are for Identity 1.0, wherea

3条回答
  •  难免孤独
    2021-01-30 09:38

    In your examples, you seem to want to replace the DbContext with something else but I believe that actually, you need to focus your efforts one level higher.

    The framework has a class UserManager which has the responsibility for managing, but not storing, the users and their related information. When it wants to store the users (or their related information) the default is to use the provided UserStore which knows how to store IdentityUser instances in a database using a DbContext.

    In the 2.0 framework, the various bits of the identity storing were broken into several interfaces. The default implementation provided, UserStore implements several of these interfaces and stores the data for them all in the database using a DBContext.

    If you look at the defnition of the default provided entity framework based UserStore you can see that it implements many of these small interfaces:

    public class UserStore : IUserLoginStore, 
    IUserClaimStore, IUserRoleStore, IUserPasswordStore, 
    IUserSecurityStampStore, IQueryableUserStore, IUserEmailStore, 
    IUserPhoneNumberStore, IUserTwoFactorStore, IUserLockoutStore, 
    IUserStore, IDisposable 
    where TUser : IdentityUser
    where TRole : IdentityRole
    where TKey : Object, IEquatable
    where TUserLogin : new(), IdentityUserLogin
    where TUserRole : new(), IdentityUserRole
    where TUserClaim : new(), IdentityUserClaim
    

    As you don't want to use Entity Framework at all, you need to provide your own implementations of some of these key interfaces which will store that data in the places you want the data to be stored. The main interface is IUserStore. this defines the contract for storing users which have a key of a particular type. If you only want to store users and no other information then you can implement this interface and then pass your implementation to the UserManager and you should be good to go.

    It's unlikely that this will be enough though as likely you will want passwords, roles, logins etc for your users. If so then you need to make your class which implements IUserStore also implement IUserPasswordStore, IRoleStore and IUserClaimStore.

    You can find a list of all the interfaces on MSDN here

    Depending on the bits you want to use you need to implement those interfaces.

    You will probably also need to define your own versions of the Identity* classes which exist for the Entity framework already out of the box. So you will need your own IdentityUser class which represents the users you want to store, and IdentityUserClaim for the users claims. I haven't done this myself so I'm not exactly sure, but my feeling is that you will have to do this.

    Scott Allen has a good article on the various bits of the model which might be useful.

    As for your issues with this line:

    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));
    

    There is a method called GenerateUserIdentityAsync which is defined on Applicationuser which extends IdentityUser. It is defined in the template project I believe and looks something like this:

    public class ApplicationUser : IdentityUser 
    {    
    public async Task GenerateUserIdentityAsync(
        UserManager manager) {
        // Note the authenticationType must match the one 
        // defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = 
            await manager.CreateIdentityAsync(this, 
                DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
        }
    }
    

    As you are not using the entity framework IdentityUser any more then you either need to define a similar method or your own User class or implement the same functionality some other way (like just calling await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie) instead)

提交回复
热议问题