我已经基于存储库模式创建了一个项目。并使用本教程实现基于令牌的身份验证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>
这显然是依赖注入问题。请指导我解决方案。
答案 0 :(得分:1)
看起来IAuthenticator没有从IDisposable继承。 使用'using'IDisposable时,确保在使用范围结束后可以使用using语句处理变量。