【发布时间】:2013-10-23 21:52:50
【问题描述】:
如何拒绝身份?我的类继承自 OAuthBearerAuthenticationProvider 并且我覆盖了 ValidateIdentity?
我已经尝试设置 context.Rejected();或 context.SetError();并抛出异常,但我的控制器仍然被调用。 OAuthBearerAuthenticationHandler 确实调用了我的类,所以我知道我的设置正确。
我当前的失败代码
public void ConfigureAuth ( IAppBuilder app )
{
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerAuthentication ( new OAuthBearerAuthenticationOptions ()
{
Provider = new OAuthBearerAuthenticationProvider ()
{
OnValidateIdentity = async ctx => { ctx.Rejected (); }
}
} );
app.UseOAuthBearerTokens(OAuthOptions);
}
【问题讨论】:
-
你的 Action Method 有 Authorize 属性吗?记住授权中间件提供了分配 ClaimsIdentity 的服务。
-
是的,我可以逐步通过授权过滤器,它的 OAuthBearerAuthenticationHandler 似乎不起作用。或者它的调用者忽略了它返回 null 的事实。
-
您是否到达分配了 AuthorizeAttribute 的控制器方法?如果返回值为 null,则 ClaimsIdentity 不会分配给当前用户。所以它就像匿名一样。
-
它通过授权过滤器并将声明身份分配给原则。控制流在调试器中很难遵循,因为它是异步代码。但我认为 OAuthBearerAuthenticationHandler 通过引用传递上下文,这在 awit 处理程序上不起作用?
标签: c# asp.net-web-api owin katana