2015-09-13 43 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

相關問題