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.
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!
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With