2017-04-07 14 views
0

我已經創建了一個基於存儲庫模式的項目。並使用本教程來實現基於令牌的認證Tutorial Link here存儲庫模式項目中基於令牌的身份驗證

這裏是我的代碼:

ValuesController.cs

public IHttpActionResult GetAuth(string Username, string Password) 
    { 
     return Ok(_au.Authenticate(Username, Password)); //returns true or false 
    } 

IAuthenticator.cs

namespace AuthSO.Core.Interface 
{ 
    public interface IAuthenticator:IDisposable 
    { 
     bool Authenticate(string Username, string Password); 
    } 
} 

Authenticator.cs

namespace AuthSO.Core.Manager 
{ 
    public class Authenticator : IAuthenticator, IDisposable 
    { 
     public IGetData _get; 
     public Authenticator(IGetData _get) 
     { 
      this._get = _get; 
     } 
     public bool Authenticate(string Username, string Password) 
     { 
      return _get.Authenticate(Username, Password); 
     } 
     public void Dispose() 
     { 
      //left empty here, nothing to dispose here i guess 
     } 
    } 
} 

AuthenticatorProvider.cs

namespace AuthSO.API.Provider 
{ 
    public class AuthenticatorProvider : OAuthAuthorizationServerProvider 
    { 
     public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
     { 
      context.Validated(); 
     } 

     public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
     { 

      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 

      using (IAuthenticator _auth = new Authenticator()) //I'm getting an error here 
      { 
       bool result = _auth.Authenticate(context.UserName, context.Password); 

       if (!result) 
       { 
        context.SetError("invalid_grant", "The user name or password is incorrect."); 
        return; 
       } 
      } 

      var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
      identity.AddClaim(new Claim("sub", context.UserName)); 
      identity.AddClaim(new Claim("role", "user")); 

      context.Validated(identity); 

     } 
    } 
} 

問題:

enter image description here

我怎樣才能解決這個問題。我正在使用Autofac進行依賴注入。

My sample project with this issue is in my github repo

任何意見將是有益的,謝謝。

更新#1:

我使用了IDisposable仍然得到我這個錯誤嘗試,請查看更新的Authenticator.cs代碼。 (TokenAuthIssue/AuthSO.Core /經理/ Authenticator.cs在github上)

enter image description here

更新#2:

我試圖用這個

using (IAuthenticator _auth = new Authenticator(new GetData())) 

但我再次遇到錯誤

<Error> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
An error occurred when trying to create a controller of type 'ValuesController'. Make sure that the controller has a parameterless public constructor. 
</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace> 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext() 
</StackTrace> 
<InnerException> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
Type 'AuthSO.API.Controllers.ValuesController' does not have a default constructor 
</ExceptionMessage> 
<ExceptionType>System.ArgumentException</ExceptionType> 
<StackTrace> 
at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
</StackTrace> 
</InnerException> 
</Error> 

這顯然是一個依賴注入問題。請指導我解決問題。

+0

你確定IAuthenticator是否實現了IDisposable?你可以發佈IAuthenticator類嗎? –

+0

錯誤非常明顯,與存儲庫模式或身份驗證無關。您試圖使用*不*實現IDisposable接口的類。 'using(...)'語句是調用'IDisposable.Dispose()'的語法糖。 –

+0

請參閱我更新的問題,我已添加IAuthenticator.cs以及IDisposable。 –

回答

1

看起來像IAuthenticator沒有從IDisposable繼承。 使用'使用'IDisposable時,確保它可以在使用範圍結束後從using語句中處理變量。

相關問題