2 回答

TA貢獻1854條經驗 獲得超8個贊
好吧,我在另一個問題中找到了解決方法。您可以通過設置 AuthenticationSchemeSelector 方法,在運行時為每個請求選擇身份驗證模式,而不是指定多個身份驗證模式(這不起作用):
public void Configuration(IAppBuilder app)
{
HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemeSelectorDelegate = new
AuthenticationSchemeSelector(GetAuthenticationScheme);
}
private AuthenticationSchemes GetAuthenticationScheme(HttpListenerRequest httpRequest)
{
if(/* some logic... */){
return AuthenticationSchemes.Anonymous;
}
else{
return AuthenticationSchemes.IntegratedWindowsAuthentication;
}
}
雖然不理想(您必須手動檢查請求 URL 或請求的其他一些參數來決定使用哪種方法)但它可以工作。

TA貢獻1817條經驗 獲得超6個贊
由于您對問題的描述有限,我已經設置了一個演示應用程序,我在其中實現OAuthAuthorizationServerProvider為 Provider forOAuthAuthorizationServerOptions和 override GrantResourceOwnerCredentialsandValidateClientAuthentication
public void Configuration(IAppBuilder app)
{
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
Provider = new ApplicationOAuthBearerAuthenticationProvider()
});
app.Use<AuthenticationResponseMiddleware>();
var options = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/xxxx"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new OwinAuthorisationProvider()
};
app.UseOAuthAuthorizationServer(options);
}
還嘗試AuthorizeAttribute在配置類中自定義并添加為過濾器.Filters.Add(new AuthorizeAttribute());
在AuthenticationResponseMiddleware我繼承OwinMiddleware的public override async Task Invoke(IOwinContext context)方法中,請檢查請求的流程。
它OAuthBearerAuthenticationProvider首先在RequestToken方法中命中,然后在OwinMiddleware類中命中,在進入任何 DelegatingHandler管道之前,大部分身份驗證都是在此層中實現的。
檢查后請評論您的發現,同時我也修改API并更新您,希望它可以幫助您。
- 2 回答
- 0 關注
- 280 瀏覽
添加回答
舉報