2 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
好吧,我在另一個(gè)問(wèn)題中找到了解決方法。您可以通過(guò)設(shè)置 AuthenticationSchemeSelector 方法,在運(yùn)行時(shí)為每個(gè)請(qǐng)求選擇身份驗(yàn)證模式,而不是指定多個(gè)身份驗(yàn)證模式(這不起作用):
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;
}
}
雖然不理想(您必須手動(dòng)檢查請(qǐng)求 URL 或請(qǐng)求的其他一些參數(shù)來(lái)決定使用哪種方法)但它可以工作。

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
由于您對(duì)問(wèn)題的描述有限,我已經(jīng)設(shè)置了一個(gè)演示應(yīng)用程序,我在其中實(shí)現(xiàn)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在配置類(lèi)中自定義并添加為過(guò)濾器.Filters.Add(new AuthorizeAttribute());
在AuthenticationResponseMiddleware我繼承OwinMiddleware的public override async Task Invoke(IOwinContext context)方法中,請(qǐng)檢查請(qǐng)求的流程。
它OAuthBearerAuthenticationProvider首先在RequestToken方法中命中,然后在OwinMiddleware類(lèi)中命中,在進(jìn)入任何 DelegatingHandler管道之前,大部分身份驗(yàn)證都是在此層中實(shí)現(xiàn)的。
檢查后請(qǐng)?jiān)u論您的發(fā)現(xiàn),同時(shí)我也修改API并更新您,希望它可以幫助您。
- 2 回答
- 0 關(guān)注
- 293 瀏覽
添加回答
舉報(bào)