【发布时间】:2016-12-31 14:01:31
【问题描述】:
我正在尝试创建一个新的 API 密钥自定义身份验证提供程序以插入我的 OWIN 管道。 我也在使用 Cookie、OAuth 和 ADFS 提供程序。 我实现的代码差不多是这样的:
public static class ApiKeyAuthenticationExtension
{
public static IAppBuilder UseApiKeyAuthentication(this IAppBuilder appBuilder, ApiKeyAuthenticationOptions options = null)
{
appBuilder.Use<ApiKeyAuthenticationMiddleware>(options ?? new ApiKeyAuthenticationOptions("ApiKey"));
appBuilder.UseStageMarker(PipelineStage.Authenticate);
return appBuilder;
}
}
public class ApiKeyAuthenticationMiddleware : AuthenticationMiddleware<AuthenticationOptions>
{
public ApiKeyAuthenticationMiddleware(OwinMiddleware next, AuthenticationOptions options) : base(next, options)
{
}
protected override AuthenticationHandler<AuthenticationOptions> CreateHandler()
{
return new ApiKeyAuthenticationHandler();
}
}
public class ApiKeyAuthenticationHandler : AuthenticationHandler<AuthenticationOptions>
{
private const string ApiKey = ".....";
protected override Task<AuthenticationTicket> AuthenticateCoreAsync()
{
string apiKey = Context.Request.Headers["ApiKey"];
if (!string.IsNullOrEmpty(apiKey) && ApiKey.Equals(apiKey))
{
var identity = new ClaimsIdentity(Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "Id", null, Options.AuthenticationType));
identity.AddClaim(new Claim(ClaimTypes.Name, "Name"));
identity.AddClaim(new Claim(ClaimTypes.Email, "bla@blu.com"));
return new Task<AuthenticationTicket>(() => new AuthenticationTicket(identity, new AuthenticationProperties()));
}
return Task.FromResult(null as AuthenticationTicket);
}
}
public class ApiKeyAuthenticationOptions : AuthenticationOptions
{
public ApiKeyAuthenticationOptions(string authenticationType) : base(authenticationType)
{
}
}
我的 Startup.Auth 看起来像这样:
app.UseCookieAuthentication(...
app.UseActiveDirectoryFederationServicesBearerAuthentication(...
app.UseOAuthAuthorizationServer(...
app.UseOAuthBearerAuthentication(...
最后
app.UseApiKeyAuthentication(...
当执行进入 AuthenticateCoreAsync 并且我返回和身份验证票时,浏览器只是挂起并且执行似乎无处可去。之后什么都没有发生。
我在这里错过了什么?
【问题讨论】:
标签: authentication asp.net-web-api2 owin api-key