ASP.NET Identity without Entity Framework

浪子不回头ぞ 提交于 2021-02-07 12:14:39

问题


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.


回答1:


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!




回答2:


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); 
}


来源:https://stackoverflow.com/questions/28452062/asp-net-identity-without-entity-framework

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