using aspnet identity with custom tables

我的未来我决定 提交于 2019-12-06 02:57:48

Good luck with that! :) I have just gone through that process last few days. I've got it to work but its really painful at some stages.

In short:

  1. You need to create your own user model that implements IUser interface.
  2. You need to create your own DAL that gets data from your custom db tables
  3. You need to implement your own UserStore that implements different interfaces based on what functionality of asp.identity you want to use

This link will help you: https://www.asp.net/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity

You can use Cookie Middleware Authentication.

In your Startup.cs you add

app.UseCookieAuthentication(new CookieAuthenticationOptions()
   {
       AuthenticationScheme = "MyCookieMiddlewareInstance",
       LoginPath = new PathString("/Account/Unauthorized/"),
       AccessDeniedPath = new PathString("/Account/Forbidden/"),
       AutomaticAuthenticate = true,
       AutomaticChallenge = true
   });

In your code, after you validate username and password, to login you call

await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);

and to signoff

await HttpContext.Authentication.SignOutAsync("MyCookieMiddlewareInstance");

Please see article at Microsoft website for more details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!