2017-05-08 34 views
0

我正在創建一個新的Web API並使用OAuth2進行令牌身份驗證,並使用UnityResolver進行DI。在這一刻,我有一些關於如何使用我自己的userRepository來認證用戶的問題。這兩天我一直在閱讀大量的關節,我主要找到的解決方案是實現IStoredUser,IUser ..對於這種情況,我認爲會更好。 這是我的代碼:使用我自己的userRepository的.NET Owin身份驗證

public class OAuthServerProvider : OAuthAuthorizationServerProvider 
{ 
    IUserAccountService _userAccountService; 

    public OAuthServerProvider(IUserAccountService userAccountService) 
    { 
     _userAccountService = userAccountService; 
    } 

    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
    { 
     context.Validated(); 
    } 

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
     var ipAddress = HttpContext.Current.Request.UserHostAddress; 
     var userAgent = HttpContext.Current.Request.UserAgent; 

     var user = await _userAccountService.GetUserByLogin(context.UserName, context.Password, 1, userAgent, ipAddress); 

     if (user != null && context.UserName == user.email && context.Password == user.password) 
     { 
      identity.AddClaim(new Claim(ClaimTypes.Role, "admin")); 
      identity.AddClaim(new Claim("username", user.email)); 
      identity.AddClaim(new Claim(ClaimTypes.Name, user.firstName)); 
      context.Validated(identity); 
     } 
     else 
     { 
      context.SetError("invalid_grant", "The user name or password is incorrect."); 
      return; 
     } 
    } 

} 

,你可以看到我使用IUserAccountService如果一個用戶在我的數據庫中調用存儲過程和短小精悍調用SP,在這個部分我沒事存在進行身份驗證。當我需要在我Startup.cs實例化這個類將出現問題

public class Startup 
{ 
    internal IUserAccountService _userAccountService; 

    public void Configuration(IAppBuilder app) 
    {    
     app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 

     var oAuthProvider = new OAuthServerProvider(_userAccountService); 

     OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions 
     { 
      AllowInsecureHttp = true, 
      TokenEndpointPath = new PathString("/api/token"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
      Provider = oAuthProvider 
     }; 
     app.UseOAuthAuthorizationServer(options); 
     app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 

     HttpConfiguration config = new HttpConfiguration(); 
     WebApiConfig.Register(config); 
    } 
} 

我會因爲IUserAccountService的空對象引用,我試圖把它注入到啓動類的構造函數,但不會工作。

您是否有任何想法如何通過使用良好實踐和DI來實施此解決方案?

回答

0

也許您已將IUserAccountService定義爲按請求生命週期範圍。 這種方式IUserAccountService在web請求開始時被實例化。 當oauth服務器啓動並且此時沒有網絡請求時,將執行Startup.Configuration()方法。

我有類似的問題,但我使用Autofac和解決它從OwinContext得到Autofac壽命範圍:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
{ 
    ... 
    // Get IUserService from AutofacLifetimeScope (e.g. 'AutofacWebRequest') 
    var autofacLifetimeScope = OwinContextExtensions.GetAutofacLifetimeScope(context.OwinContext); 
    var userService = autofacLifetimeScope.Resolve<IUserService>(); 
    ... 
} 

這樣,你從Web請求的壽命範圍IUserService實例。

我不知道你是否可以用Unity做類似的事情。

我希望它有幫助。