在演示模式下允许所有 api controlleraction 请求
Allow all api controlleraction requests when in demo mode
我有一个网站api,我在其中使用属性 [AllowAnonymous] 和 [AuthorizeAttribute] 来控制访问。我还创建了一个自定义属性来为授权添加一些逻辑。
网络 api 正在使用不记名令牌进行身份验证。
我的项目中有一个设置(一个名为 InDemo 的布尔值),目的是使我的所有操作都允许匿名请求,换句话说,就像所有操作都具有 [AllowAnonymous] 属性一样。
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider("self"),
AccessTokenExpireTimeSpan = TimeSpan.FromSeconds(30000),
AllowInsecureHttp = true
};
app.UseOAuthBearerTokens(OAuthOptions);
public class CustomApiAuthorizeAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (Settings.Default.InDemo)
return true;
// ... more custom validations
}
}
只要我的请求包含有效的不记名令牌,它就可以正常工作,然后调用 IsAuthorized 并且我可以绕过自定义验证。但是,如果令牌无效,则永远不会调用 IsAuthorized 并发送 "Authorization has been denied for this request" 响应。
现在我想在 InDemo 设置为 true 时忽略令牌,即具有 [AllowAnonymous].
的行为
好的,我是这样解决的。
我让我的 CustomApiAuthorizeAttribute 实现了 IAuthenticationFilter 并将上下文原则设置为始终经过身份验证的原则。
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
if (Settings.Default.AllowAnonymous)
context.Principal = new AuthenticatedPrincipal(Thread.CurrentPrincipal);
}
public class AuthenticatedPrincipal : IPrincipal
{
private readonly IPrincipal principalToWrap;
public AuthenticatedPrincipal(IPrincipal principalToWrap)
{
this.principalToWrap = principalToWrap;
Identity = new AuthenticatedIdentity(principalToWrap.Identity);
}
public bool IsInRole(string role)
{ return principalToWrap.IsInRole(role); }
public IIdentity Identity { get; }
}
public class AuthenticatedIdentity : IIdentity
{
public AuthenticatedIdentity(IIdentity identityToWrap)
{
Name = identityToWrap.Name;
AuthenticationType = identityToWrap.AuthenticationType;
}
public string Name { get; }
public string AuthenticationType { get; }
public bool IsAuthenticated => true;
}
我有一个网站api,我在其中使用属性 [AllowAnonymous] 和 [AuthorizeAttribute] 来控制访问。我还创建了一个自定义属性来为授权添加一些逻辑。 网络 api 正在使用不记名令牌进行身份验证。 我的项目中有一个设置(一个名为 InDemo 的布尔值),目的是使我的所有操作都允许匿名请求,换句话说,就像所有操作都具有 [AllowAnonymous] 属性一样。
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider("self"),
AccessTokenExpireTimeSpan = TimeSpan.FromSeconds(30000),
AllowInsecureHttp = true
};
app.UseOAuthBearerTokens(OAuthOptions);
public class CustomApiAuthorizeAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (Settings.Default.InDemo)
return true;
// ... more custom validations
}
}
只要我的请求包含有效的不记名令牌,它就可以正常工作,然后调用 IsAuthorized 并且我可以绕过自定义验证。但是,如果令牌无效,则永远不会调用 IsAuthorized 并发送 "Authorization has been denied for this request" 响应。 现在我想在 InDemo 设置为 true 时忽略令牌,即具有 [AllowAnonymous].
的行为好的,我是这样解决的。 我让我的 CustomApiAuthorizeAttribute 实现了 IAuthenticationFilter 并将上下文原则设置为始终经过身份验证的原则。
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
if (Settings.Default.AllowAnonymous)
context.Principal = new AuthenticatedPrincipal(Thread.CurrentPrincipal);
}
public class AuthenticatedPrincipal : IPrincipal
{
private readonly IPrincipal principalToWrap;
public AuthenticatedPrincipal(IPrincipal principalToWrap)
{
this.principalToWrap = principalToWrap;
Identity = new AuthenticatedIdentity(principalToWrap.Identity);
}
public bool IsInRole(string role)
{ return principalToWrap.IsInRole(role); }
public IIdentity Identity { get; }
}
public class AuthenticatedIdentity : IIdentity
{
public AuthenticatedIdentity(IIdentity identityToWrap)
{
Name = identityToWrap.Name;
AuthenticationType = identityToWrap.AuthenticationType;
}
public string Name { get; }
public string AuthenticationType { get; }
public bool IsAuthenticated => true;
}