【问题标题】:Custom Asp.net mvc 5 authentication without using Microsoft.AspNet.Identity.EntityFramework不使用 Microsoft.AspNet.Identity.EntityFramework 的自定义 Asp.net mvc 5 身份验证
【发布时间】:2014-08-25 14:55:33
【问题描述】:

如何在不使用 Microsoft.AspNet.Identity.EntityFramework 的 UserStore 的情况下创建自定义 ASP.NET MVC 5 Auth??

【问题讨论】:

  • 使用通用供应商?未使用 MVC5 测试,但 with MVC4
  • 你想避免 EF 吗? Shoe 为您提供了如何实现自己的 UserStore 的存根,这可能不依赖于 EF。虽然实现起来很麻烦......
  • 我发现大多数不想使用Identity的人要么有非常具体的要求,要么他们不理解它并认为它的工作量太大(绝对不是,它非常简单,自己实现它的工作要多得多)

标签: c# asp.net asp.net-mvc asp.net-mvc-5 owin


【解决方案1】:

您所要做的就是实现Identity.Entityframework 的用户存储所使用的相同接口。

User 将是您的用户类

public class MyUserStore<TUser> : 
    IUserLoginStore<TUser, int>, 
    IUserClaimStore<TUser, int>, 
    IUserRoleStore<TUser, int>, 
    IUserPasswordStore<TUser, int>, 
    IUserSecurityStampStore<TUser, int>, 
    IUserStore<TUser, int>, 
    IDisposable where TUser : User
{
   //Implement the interfaces you need
}

然后将您的MyUserStore 传递给UserManager 每个请求

new UserManager<User, int>(new MyUserStore<User>(new MyContext()))

【讨论】:

  • 这有点工作.. 但是谢谢.. 还有其他方法可以让登录/注销变得简单吗? Asp.NET MVC 5 Default AccountController 看起来很复杂。我只有一个简单的用户类,用户名和密码存储在数据库中。
  • 您想让它变得简单还是不使用Identity.Entityframework?因为这个命名空间真的是为你做一堆繁重的工作。
  • 有自定义这些东西的指南吗?
  • 简单并且不想使用 Identity.EntityFramework.. 我想将它调整到我的应用程序我已经有我的 DbContexts.. 和一个 DbSet User 类是我写的..
  • 真的没有办法。您可以使用来自 identity.entityframework 的连接来识别身份,或者实现自己的连接。另一种选择是完全放弃用户管理器并手动创建身份。
【解决方案2】:

您可以从 GitHub 上的以下项目中获取 UserStore.cs 模板并根据需要对其进行调整,它将使您摆脱对 Microsoft.AspNet.Identity.EntityFramework 的依赖。

https://github.com/kriasoft/AspNet-Server-Template -> ./src/App.Server/Data/UserStore.cs

(免责声明:我是这个项目模板的作者)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多