title: Token based authentication in repository pattern project
body: <p>I have created a project based on repository pattern. And used this tutorial to implement token based authentication <a href="http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/" rel="nofollow noreferrer">Tutorial Link here</a>. </p>
  
  <p>Here's my code:</p>
  
  <p><strong>ValuesController.cs</strong></p>
  
  <pre><code>public IHttpActionResult GetAuth(string Username, string Password)
      {
          return Ok(_au.Authenticate(Username, Password)); //returns true or false
      }
  </code></pre>
  
  <p><strong>IAuthenticator.cs</strong></p>
  
  <pre><code>namespace AuthSO.Core.Interface
  {
      public interface IAuthenticator:IDisposable
      {
          bool Authenticate(string Username, string Password);
      }
  }
  </code></pre>
  
  <p><strong>Authenticator.cs</strong></p>
  
  <pre><code>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
          }
      }
  }
  </code></pre>
  
  <p>AuthenticatorProvider.cs</p>
  
  <pre><code>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);
  
          }
      }
  }
  </code></pre>
  
  <p>Issue :</p>
  
  <p><a href="https://i.stack.imgur.com/v8Adk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v8Adk.png" alt="enter image description here"></a></p>
  
  <p>How can i solve this issue. I'm using Autofac for dependency injection. </p>
  
  <p><a href="https://github.com/darthranjith/TokenAuthIssue" rel="nofollow noreferrer">My sample project with this issue is in my github repo</a></p>
  
  <p>Any advice would be helpful, Thank you.</p>
  
  <p><strong>Update #1:</strong></p>
  
  <p>I tried using IDisposable still gets me this error, please see the updated Authenticator.cs code. (TokenAuthIssue/AuthSO.Core/Manager/Authenticator.cs in github)</p>
  
  <p><a href="https://i.stack.imgur.com/MzLUN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MzLUN.png" alt="enter image description here"></a></p>
  
  <p>Update #2:</p>
  
  <p>I tried using this</p>
  
  <pre><code>using (IAuthenticator _auth = new Authenticator(new GetData()))
  </code></pre>
  
  <p>but again i got error </p>
  
  <pre><code>&lt;Error&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  An error occurred when trying to create a controller of type 'ValuesController'. Make sure that the controller has a parameterless public constructor.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  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.&lt;SendAsync&gt;d__1.MoveNext()
  &lt;/StackTrace&gt;
  &lt;InnerException&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  Type 'AuthSO.API.Controllers.ValuesController' does not have a default constructor
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.ArgumentException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  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&amp; activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  &lt;/StackTrace&gt;
  &lt;/InnerException&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>This is clearly a dependency injection issue. Please guide me to the solution.</p>