我有一个名为AuthenticatToken的类,它看起来像这样
'-'.join(string.rsplit('-',)[1:-2])
问题是,当在控制器中调用上述类作为自定义数据注释时,变量未初始化,它使用先前的值进行第二次对控制器的请求。
public class TokenBasedAuthentication : ActionFilterAttribute, IAuthenticationFilter
{
ILocalData local = null;
string token = string.Empty;
bool istokenvalid = false;
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
istokenvalid = false;
await Task.Factory.StartNew(() =>
{
var req = context.Request;
IEnumerable<string> headervalues = new List<string>();
bool hastoken = req.Headers.TryGetValues(Constants.TOKEN, out headervalues);
if (hastoken)
{
local = new LocalData();
token = headervalues.FirstOrDefault();
istokenvalid = local.ValidateToken(token);
}
if (!istokenvalid)
{
context.ErrorResult = new AuthenticationFailureResult("Invalid token when accessing service", req);
}
else
{
IPrincipal incomingprincipal = context.ActionContext.RequestContext.Principal;
context.Principal = incomingprincipal;
}
});
}
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}
}
所以我的问题是数据注释是静态的吗?