2015-09-13 123 views
7

根本没有文档。如何实现ASP.NET的NoSQL身份提供程序5 MVC 6

我知道我必须实现我自己的IUser,我自己的IUserSTore,并以某种方式在startup.cs中注册它们。我删除了对EntityFramework的所有引用,因为我想使用NoSQL后端。

只要有文件记录和公开,“公约”理念是非常好的。

任何提示?

+0

看起来非常类似于这个问题http://stackoverflow.com/questions/31795792/example-of-using-asp-net-identity-3-0-without-entity-framework/31813863 –

+0

谢谢。我正在学习启发式。小心不要使用传统的标识包。这是我的情况。现在我正在解决我在AddIdentity中的自定义UserStore和RoleStore注册之后的例外().AddUserStore ().AddRoleStore ().AddDefaultTokenProviders(); – user2715109

+1

这是一个很好的工作示例(MVC 6),并且带有用于MongoDB.Driver(> = v2.1.0)的ASP.NET 5 Identity(> = v3)框架的实现lib https:// github。 com/saan800/SaanSoft.AspNet.Identity3.MongoDB –

回答

8

我刚刚做了Identity 2.0的自定义实现,正如您所说,我没有找到任何有用的文档。幸运的是,我设法实现了我的目标。

假设您使用的是N层架构,将您的业务逻辑与业务逻辑与数据访问层隔离,我会回答您的问题。并假设有一个正在使用的依赖注入容器,如Unity。

我会解释您必须遵守使用Identity框架与定制数据访问的步骤:

首先,你必须声明你的域类,实施IUSER,如果你愿意,添加自定义属性,它:

//This class is implementing IUser with Guid as type because 
//I needed to use Guid as default Id. 
public class CustomUser : IUser<Guid> 
{ 
     public string CustomProperty { get; set; } 
} 

然后,在你的业务逻辑层,你应该有一个处理相关的用户授权,登录名,密码恢复,以及其他所有任务的类。该类必须从UserManager继承。其结果将是什么如下:

// Business layer class must inherit from UserManager with 
// CustomUser and Guid as types 
public AuthorizationManager : UserManager<CustomUser, Guid>, IAuthorizationManager 
{ 
     private readonly ICustomUserMongoRepository repository; 
     private readonly ICustomEmailService emailService; 
     private readonly ICustomTokenProvider tokenProvider; 

     // Parameters being injected by Unity. 
     // container.RegisterType<ICustomUserMongoRepository, CustomUserMongoRepository>(); 
     // .. 
     // .. 
     public AuthorizationManager(
        ICustomUserMongoRepository repository, 
        ICustomEmailService emailService, 
        ICustomTokenProvider tokenProvider 
        ) 
           // calling base constructor passing 
           // a repository which implements 
           // IUserStore, among others. 
           : base(repository) 
     { 
      this.repository = repository; 

      // this.EmailService is a property of UserManager and 
      // it has to be set to send emails by your class 
      this.EmailService = emailService; 

      // this.UserTokenProvider is a property of UserManager and 
      // it has to be set to generate tokens for user password 
      // recovery and confirmation tokens 
      this.UserTokenProvider = tokenProvider; 
     } 
} 

当从的UserManager继承,它将提供一系列由身份所使用的方法,它会迫使你的类调用基构造函数传递一个信息库,但没有任何资料库,存储库必须实现接口:IUserStore,IPasswordStore,具体取决于您的要求。

这是酷的事情发生时。在你的数据访问层中,你必须有你自定义的存储库模式实现,连接到NoSQL数据库(让我们假设它是Mongo)。所以,你的ICustomUserMongoRepository应该是这个样子:

public interface ICustomUserMongoRepository : IUserPasswordStore<CustomUser, Guid>, IUserEmailStore<CustomUser, Guid>, IUserRoleStore<CustomUser, Guid> 
{ 
} 

和你蒙戈库应该是这样的

public CustomUserMongoRepository : MongoRepository<CustomUser>, ICustomUserMongoRepository 
{ 
     // Here you must have your custom implementation (using Mongo) of 
     // ICustomUserRepository which is requesting your class to 
     // implement IUserPasswordStore methods as well 

     public Task CreateAsync(CustomUser user) 
     { 
      //Custom Mongo implementation 
     } 

     public Task DeleteAsync(CustomUser user) 
     { 
      //Custom Mongo implementation 
     } 

     public Task GetEmailAsync(CustomUser user) 
     { 
      //Custom Mongo implementation 
     } 

     public Task GetEmailConfirmedAsync(CustomUser user) 
     { 
      //Custom Mongo implementation 
     } 

     // ... 
} 

然后最后你的控制器会是这个样子:

public class AuthController : Controller 
{ 

    private readonly IAuthorizationManager manager;   

     // Manager being injected by Unity. 
     // container.RegisterType<IAuthorizationManager, AuthorizationManager>(); 
    public AuthController(IAuthorizationManager manager) 
    { 
      this.manager = manager; 
    } 

    // Receives a LogInViewModel with all data needed to allow users to log in 
    [HttpPost] 
    public async Task<ActionResult> LogIn(LogInViewModel viewModel) 
    { 
     // FindAsync it's a method inherited from UserManager, that's 
     // using the Mongo repository passed to the base class 
     // in the AuthorizationManager constructor 
     var user = this.manager.FindAsync(viewModel.Email, viewModel.Password); 

     if(user != null){ // Log in user and create user session } 
     else { // Wrong username or password } 

    } 
} 

重要!

接口IAuthorizationManager它用于提供基于SOLID原则的高质量软件。如果您仔细观察并仔细考虑,您将注意到此接口必须具有所有UserManager的方法才能允许AuthController通过UserManager类的AuthorizationManager调用所有继承的方法

对不起长的职位。很难用几行来解释整个过程。我希望它有帮助。如果您有任何疑问或问题,请评论此答案,我会尽快回复。

+1

谢谢!我已经实现了Identity 3的Identity类,它比Identity 2更容易。您只需要在Identity 3中重写UserStore和RoleStore! – user2715109

+0

更好。问候 – wilsotobianco

相关问题