11

Is it possible to use the new ASP.NET Identity without using Entity Framework and instead use your own methods?

I have an MVC project that uses plain ADO.NET for data access. I want to implement ASP.NET identity but I would like to continue to use ADO.NET and stored procedures. This is the way that I have chosen to go.

2
  • This is a good and working, example (MVC 6) and lib of implementation with ASP.NET 5 Identity (>= v3) framework without Entity Framework for MongoDB.Driver (>= v2.1.0) github.com/saan800/SaanSoft.AspNet.Identity3.MongoDB Commented Sep 20, 2015 at 13:52
  • Hi Brendan, did you got you answer or what helped you. Commented Sep 13, 2022 at 1:40

2 Answers 2

6

I am using Asp.Net Identity 2.2.1. The way I implemented this was by creating my own User:

public class MyUser : IUser {...}

Then create my own UserStore: (Implement all the methods required by the interfaces you implemented, and on those methods pull/insert the data using your data access. For example I created another dal class on which I use dapper to access the database, I provided the example of one method in my user store, but it is the same idea for all other methods you implement)

public class MyUserStore : IUserStore<MyUser>, IUserPasswordStore<MyUser>, IUserLockoutStore<MyUser, string>, IUserEmailStore<MyUser>
{
    public Task<MyUser> FindByNameAsync(string userName)
    { 
        MyUser muser = dal.FindByUsername(userName);

        if (muser != null)
            return Task.FromResult<User>(muser);

        return Task.FromResult<MyUser>(null);
    }
    //... all other methods
}

Then the method on my dal.cs looks something like this:

public MyUser FindByUsername(string username)
{
    //pull user from the database however you want to based on the username, 
    //in my case here is where I used Dapper to query the db and return the User object with the data on it, but you can do it however you want.

    return myUser; //object with data on it
}

Then on my UserManager I set the user store like this:

public UserManager() : base(new MyUserStore())
{
    //anything else you need on the constructor
}

NOTE: Not all the methods on MyUserStore need to access the database, because a lot of them call the FindUser... methods or the UpdateUser method. So for example:

public Task<int> IncrementAccessFailedCountAsync(MyUser user)
{
    //here I just updated the user object as I know that UpdateAsync() method will be called later.
    return Task.FromResult<int>(++user.FailedAttempts); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please share code ApplicationUserManager and ApplicationSignInManager. I implemented MyUserStore but facing problem in implementing above 2 classes. I'm also using dapper.
ApplicationUserManager: public class ApplicationUserManager: UserManager<User> { public ApplicationUserManager() : base(new MyUserStore()) { ... } ... } On the constructor you have to set the UserStore to your implementation as the parameter, in this case MyUserStore. I think that is the only important part in the App User Manager, it is too long to add here; but the rest I have is just configuration for the PasswordValidator, dataProtectionProvider, and EmailService, but that is the same code as the template
You don't need that many changes on the ApplicationUserManager. Since the code that access the data is the one on the UserStore, so that is were your implementation goes
4

I had similar requirements to yourself and have implemented a pure SQL Server version of the ASP.NET Identity framework.

I started out by creating a sample project (using entity framework) and then observing the tables it created. I then ported these over to a Visual Studio Sql Project.

Next, I used this link to provide guidance on which methods need implementing and which methods interact with the database (note: not all methods on the stores do/should). The code on the link is for MySQL but gave me a good understanding on what to implement etc.

I still have the code I wrote for the ASP.Net Identity Framework using Sql. So if I get time over the weekend I'll see how good a state its in and possibly upload it to github and update here with a link.

But in the mean time, use the link - it provides a good learning experience also!

1 Comment

Did you get your version uploaded?

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.