简体   繁体   English

ASP.NET身份-自定义身份验证的步骤

[英]ASP.NET Identity - Steps for custom authentication

Envrionment: Visual Studio 2013, ASP.NET MVC 5 环境:Visual Studio 2013,ASP.NET MVC 5

On the new MVC5-based project I will be working on, I need to use a custom database that stores usernames, passwords, and roles in its own way. 在我将要进行的基于MVC5的新项目中,我需要使用一个自定义数据库,该数据库以自己的方式存储用户名,密码和角色。 I am searching the Internet to look for an example for custom authentication. 我正在搜索Internet,以查找自定义身份验证的示例。 Looks like the old-style "membership provider" classes have been replaced by the new "Identity" mechanism. 看起来旧式的“成员资格提供者”类已被新的“身份”机制代替。

However, finding a good step-by-step example has proven to be futile. 但是,找到一个好的分步示例已被证明是徒劳的。 There are a few links (published this year) that talk about implementing custom IPrincipal and DbContext classes. 有一些链接(今年发布)谈论实现自定义IPrincipalDbContext类。 Some other links talk about implementing IUserLoginStore and IUserPasswordStore . 其他一些链接谈论实现IUserLoginStoreIUserPasswordStore A few others hinted on implementing IUser , IUserStore interfaces. 其他一些建议实现IUserIUserStore接口。

Maybe the last option is what is needed. 也许最后的选择是所需的。 Can someone please guide me with the steps or point me to any link that has a simple example? 有人可以指导我执行这些步骤,还是可以指向带有简单示例的任何链接? Something like: 就像是:

  1. Implement MyUser based on IUser 基于IUser实现MyUser
  2. Implement MyUserStore based on IUserStore 实施MyUserStore基于IUserStore
  3. Modify web.config to use MyUserStore 修改web.config以使用MyUserStore
  4. Remove DefaultConnection from web.config as it is not required web.config删除DefaultConnection ,因为它不是必需的

Regards. 问候。

First, stop. 首先,停下来。 Stop thinking about "custom authentication". 不要再考虑“自定义身份验证”了。 You don't need custom authentication, you just need custom storage of authentication data. 您不需要自定义身份验证,只需要自定义存储身份验证数据即可。

ASP.NET Identity has abstracted out the storage mechanism of authentication from the process of authentication. ASP.NET Identity已从身份验证过程中抽象出身份验证的存储机制。 There are several interfaces that follow the pattern IxxxStore.. Such as IUserStore, IRoleStore, etc... 有几个遵循IxxxStore模式的接口。例如IUserStore,IRoleStore等。

You can find more information about this here, along with custom implementations for various databases which you can probably convert to your own needs. 您可以在此处找到关于此的更多信息,以及各种数据库的自定义实现,您可能可以将其转换为自己的需求。

http://odetocode.com/blogs/scott/archive/2014/01/20/implementing-asp-net-identity.aspx http://odetocode.com/blogs/scott/archive/2014/01/20/implementing-asp-net-identity.aspx

As an example, here is a RavenDB implementation that uses all the various interfaces in a single class. 例如,这是一个RavenDB实现,它在一个类中使用所有各种接口。

https://github.com/tugberkugurlu/AspNet.Identity.RavenDB/blob/master/src/AspNet.Identity.RavenDB/Stores/RavenUserStore.cs https://github.com/tugberkugurlu/AspNet.Identity.RavenDB/blob/master/src/AspNet.Identity.RavenDB/Stores/RavenUserStore.cs

However, all this assumes you really truly a need to store data totally differently. 但是,所有这些都假设您确实确实确实需要完全不同地存储数据。 If you just need to store the data in different columns, then it may simply be as easy as overriding OnModelCreating in your IdentityContext and changing the names of the columns in use. 如果只需要将数据存储在不同的列中,那么它就像在IdentityContext中覆盖OnModelCreating并更改使用中的列的名称一样简单。

ad.1. ad.1。

public class ApplicationUser :IUser
    {
        public string Id
        {
            get; 
            set;
        }

        public string UserName
        {
            get;
            set;
        }
    }

ad.2. ad.2。

 public class MyStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IUserSecurityStampStore<ApplicationUser>, IUserEmailStore<ApplicationUser>
    {
... //implement all interfaces here 
}

ad. 广告。 3. Now you can create your applicationUserManagerService (you will need IdentityMessageService, and IDataProtectionProvider): 3.现在,您可以创建applicationUserManagerService(将需要IdentityMessageService和IDataProtectionProvider):

 var applicationUserManagerService = new ApplicationUserManagerService(new MyStore(), emailService, dataProtectionProvider);

Try to use IoC and register your IUserStore (I did it this way - link below): 尝试使用IoC并注册您的IUserStore(我是这样做的-下面的链接):

unityContainer.RegisterType<IUserStore<ApplicationUser>, MyStore>();

check also my answer here (i'm using int as UserId there): AspIdentiy ApplicationUserManager is Static, how to extend so it participates in my IoC framework? 在这里也检查我的答案(我在那里使用int作为UserId): AspIdentiy ApplicationUserManager是静态的,如何扩展以便它参与我的IoC框架?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM