3

I am working on asp.net MVC core application. I have custom database with users and roles tables. I want to use asp.net identity with custom tables so that I don't have to use aspnetusers, aspnet roles tables. How to do it with asp.net identity and asp.net core

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is it alternate to asp.net identity or aspnet identity with my own tables?
i am not sure i understand your question. could you please explain what you mean?
aha, ok. no, it is not alternative to asp.net identity. it is asp.identity using your own database. that is the whole point: asp.identity internaly uses its interface and then you have to implement interfaces to communicate to your custom database in order to tell asp.identity how to use it. Just as the first sentence on the link says: "ASP.NET Identity is an extensible system which enables you to create your own storage provider and plug it into your application without re-working the application. "
The link provided is to an article written for IdentityServer 2.0, not the latest version, which currently stands at 4.0. There may be differences in what is required, but it is a good starting point.
1

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

2 Comments

How to handle registration in this case? Is it alternate to asp.net identity or aspnet identity with my own tables?
You would have to use your own registration with this method. Your user and role tables can look like anything with no ristrictions.

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.