AspNet Identity Owin

AspNet Identity Owin

So lately I being playing around with AspNet Identity.

Number of things I have found out that:-

  • SignInManager was responsible for creating authentication cookie.
  • SignInManager constructor need a UserManager ==> SignInManager(UserManager x)
  • UserManager requires a implementation of IUserStore
  • IUserStore uses Entity Framework User Store

IUser<TKey>

Using the existing SigninManager as a reference,  a IUser<TKey>.

publicclassCustomUser:IUser<string>{publicstringId{get;set;}publicstringUserName{get;set;}}

 

UserManager<TUser>

Next the SigninManager constructor takes a Usermanager.

publicclassCustomUserManager:UserManager<CustomUser>{publicCustomUserManager(IUserStore<CustomUser>store):base(store){}}

 

SigninManager<TUser, TKey>

publicclassCustomSignInManager:SignInManager<CustomUser,string>{publicCustomSignInManager(CustomUserManageruserManager,IAuthenticationManagerauthenticationManager):base(userManager,authenticationManager){}}C

 

IUserStore<TUser>

The astute reader will have notice that we missed the implementation of the IUserStore<TUser> for the CustomerUserManager. This again is a very quick implementation of a few CRUD operations:

publicclassCustomUserStore:IUserStore<CustomUser>{privateCustomDbContextdatabase;publicCustomUserStore(){this.database=newCustomDbContext();}publicvoidDispose(){this.database.Dispose();}publicTaskCreateAsync(CustomUseruser){// TODO thrownewNotImplementedException();}publicTaskUpdateAsync(CustomUseruser){// TODO thrownewNotImplementedException();}publicTaskDeleteAsync(CustomUseruser){// TODO thrownewNotImplementedException();}publicasyncTask<CustomUser>FindByIdAsync(stringuserId){CustomUseruser=awaitthis.database.CustomUsers .Where(c=>c.UserId==userId).FirstOrDefaultAsync();returnuser;}publicasyncTask<CustomUser>FindByNameAsync(stringuserName){CustomUseruser=awaitthis.database.CustomUsers .Where(c=>c.UserName==userName).FirstOrDefaultAsync();returnuser;}}

 

Leave a Reply

Your email address will not be published. Required fields are marked *