ich auf einer Web-API-Anwendung arbeite, und ich habe Anforderung für authentifizierte Benutzer wie folgt zu überprüfen:Wie kann die Windows-Authentifizierung in Verbindung mit der Owin-Token-Authentifizierung in Web-API aktiviert werden?
1) Authentifizieren Benutzer mit Windows-Authentifizierung
2) Wenn nicht in Windows authentifiziert. Ich werde versuchen, Benutzer mit Owin Access-Token zu authentifizieren.
Mein Code funktioniert, aber wenn ich die Windows-Authentifizierung aktivieren, wie folgend:
public static IAppBuilder EnableWindowsAuthentication(this IAppBuilder app)
{
if (!app.Properties.TryGetValue("System.Net.HttpListener", out var val))
return app;
if (val is HttpListener listener)
{
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
}
return app;
}
Dann innen Startup.cs:
public void Configuration(IAppBuilder app)
{
OnAppDisposing(app);
ConfigureOAuth(app);
var config = new HttpConfiguration();
var webApiConfiguration = WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.EnableWindowsAuthentication();
//here some owin middlewares
app.UseWebApi(webApiConfiguration);
}
private void ConfigureOAuth(IAppBuilder app)
{
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
app.UseOAuthAuthorizationServer(OAuthServerOptions);
}
Wenn ich versuche, einen autorisierten Endpunkt unter Verwendung von Bearer-Token zu nennen ich 401 UnAuthorized.
Also meine Frage ist: Wie um dieses Szenario zu arbeiten und beide Authentifizierungsmethoden zusammenarbeiten?