1
OWINパイプラインに接続するための新しいAPIキーカスタム認証プロバイダを作成しようとしています。 私はCookie、OAuth、ADFSプロバイダも使用しています。私が実装しました コードはかなりこれです:実行に入るとOWINを使用したASP.NET Web API - カスタム認証
app.UseCookieAuthentication(...
app.UseActiveDirectoryFederationServicesBearerAuthentication(...
app.UseOAuthAuthorizationServer(...
app.UseOAuthBearerAuthentication(...
と終了
app.UseApiKeyAuthentication(...
で:
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は、このようになりますAuthenticateCoreAsyncと私は戻り、認証チケット、ブラウザはちょうどハングし、実行はどこにも行かないようです。それ以降は何も起こりません。
私はここで何が欠けていますか?
スポットオン!あまりにも長いために同じコードを見て:) –