Identity 2.0 with custom tables

前端 未结 3 450
予麋鹿
予麋鹿 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:50

    I am pretty new to Identity myself. Does your User entity implement the IUser interface? e.g.

    public partial class User : IUser //Whatever your key is
    {
        public Task GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            return Task.FromResult(GenerateUserIdentity(manager));
        }
    
        public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }
    

    Then to sign in you can call the above:

    public async Task SignInAsync(User user, bool isPersistent)
        {
            var userIdentity = await user.GenerateUserIdentityAsync(UserManager);
            AuthenticationManager.SignIn(
                new AuthenticationProperties
                {
                    IsPersistent = isPersistent
                },
                userIdentity
            );
        }   
    

提交回复
热议问题