【发布时间】:2017-09-02 22:52:35
【问题描述】:
我创建了一个基于存储库模式的项目。并使用本教程实现了基于令牌的身份验证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);
}
}
}
问题:
我该如何解决这个问题。我正在使用 Autofac 进行依赖注入。
My sample project with this issue is in my github repo
任何建议都会有所帮助,谢谢。
更新 #1:
我尝试使用 IDisposable 仍然收到此错误,请参阅更新的 Authenticator.cs 代码。 (github中的TokenAuthIssue/AuthSO.Core/Manager/Authenticator.cs)
更新 #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>
这显然是一个依赖注入问题。请指导我解决问题。
【问题讨论】:
-
你确定 IAuthenticator 实现了 IDisposable 吗?你能发布 IAuthenticator 类吗?
-
错误很明显,与存储库模式或身份验证无关。您尝试使用不实现 IDisposable 接口的类。
using(...)语句是调用IDisposable.Dispose()的语法糖。 -
请查看我更新的问题,我添加了 IAuthenticator.cs 和 IDisposable。
-
您是否阅读了收到的错误消息? "Type 'AuthSO.API.Controllers.ValuesController' 没有默认构造函数" 这与
IDisposable或IAuthenticator无关。跨度> -
请查看此链接ValuesController。我注入了一个依赖项,空的构造函数也不起作用。
标签: c# asp.net asp.net-mvc-5 asp.net-web-api2 autofac