Я пытаюсь создать новый поставщик аутентификации API-ключа API для подключения к моему конвейеру OWIN. Я использую также Cookie, OAuth и ADFS. код я реализовал это в значительной степени это:ASP.NET Web Api с OWIN - выборочная аутентификация
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, "[email protected]"));
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, и я возвращаю билет и аутентификацию, браузер просто зависает, и выполнение, похоже, никуда не годится. после этого ничего не происходит.
Что мне здесь не хватает?
Пятно на! Глядя на тот же код слишком долго :) –