Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Identity without Entity Framework

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.

like image 698
Brendan Vogt Avatar asked Feb 11 '15 10:02

Brendan Vogt


2 Answers

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!

like image 178
ry8806 Avatar answered Nov 09 '22 21:11

ry8806


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); 
}
like image 30
Divi G Avatar answered Nov 09 '22 23:11

Divi G