- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的服务器设置自己的自定义身份验证。但是它会为每个端点调用,即使它在方法上具有 [AllowAnonymous] 属性也是如此。使用我当前的代码,我每次都可以在 HandleAuthenticateAsync 方法中设置断点,即使在允许匿名函数时也是如此。
AddCustomAuthentication 正确添加了 authenticationhandler
public void ConfigureServices(IServiceCollection services)
{
//services.AddAuthorization();
services.AddAuthentication(options =>
{
// the scheme name has to match the value we're going to use in AuthenticationBuilder.AddScheme(...)
options.DefaultAuthenticateScheme = "scheme";
options.DefaultChallengeScheme = "scheme";
})
.AddCustomAuthentication(o => { });
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();
}
...
public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthenticationOptions>
{
public RvxAuthenticationHandler(
IOptionsMonitor<RvxAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock) : base(options, logger, encoder, clock)
{
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
var token = Request.Headers["token"].ToString();
if (string.IsNullOrWhiteSpace(token))
{
return AuthenticateResult.Fail("Invalid Credentials");
}
return AuthenticateResult.Success(new AuthenticationTicket(new System.Security.Claims.ClaimsPrincipal(), "Hi"));
}
最佳答案
将此添加到 HandleAuthenticateAsync
方法的顶部
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
var endpoint = Context.GetEndpoint();
if (endpoint?.Metadata?.GetMetadata<IAllowAnonymous>() != null)
{
return Task.FromResult(AuthenticateResult.NoResult());
}
....
}
这是 Microsoft 在 AuthorizeFiler
中使用的内容 - https://github.com/dotnet/aspnetcore/blob/bd65275148abc9b07a3b59797a88d485341152bf/src/Mvc/Mvc.Core/src/Authorization/AuthorizeFilter.cs#L236
它将允许您在 Controller 中使用 AllowAnonymous
属性来绕过您的自定义 AuthenticationHandler
。
关于c# - 当方法具有 [AllowAnonymous] 时调用自定义 AuthenticationHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57299922/
我在我的 asp.net web api 中设置了一个客户授权属性 全局文件: FilterConfig.RegisterHttpFilters(GlobalConfiguration.Confi
一切正常,直到我安装(包管理器控制台)邮政包,然后卸载并安装旧版本。 现在我得到了一个以前没有的错误。错误: 找不到类型或命名空间名称“AllowAnonymous”(是否缺少 using 指令或程序
AllowAnonymous 和OverrideAuthorizeAttribute 的使用有什么区别。一样吗? 最佳答案 http://www.asp.net/web-api/overview/se
我想了解 [AllowAnonymous] 标签的工作原理。 我有以下方法 [HttpGet] public ActionResult Add() { return View(); } 当我没
我正在使用自定义过滤器(定义如下): if (user == null || !user.Active) { filterContext.Res
我试图阻止特定角色(比如 RoleA)中的特定用户访问特定操作。允许匿名用户访问,但不允许 RoleA 中的用户访问该操作。 所以我做了这样的事情: [AllowAnonymous] [CustomA
我正在尝试为我的服务器设置自己的自定义身份验证。但是它会为每个端点调用,即使它在方法上具有 [AllowAnonymous] 属性也是如此。使用我当前的代码,我每次都可以在 HandleAuthent
我有一个 CustomApiAuthorizeAttribute: public class CustomApiAuthorizeAttribute : AuthorizeAttribute {
我已经创建了一个 Blazor WebAssembly 项目,并希望提供一个具有一个公共(public)可用函数的 WebAPI。 [Route("api/[controller]")] [ApiCo
我有一个建立在 Umbraco 上的整个网站,需要限制对所有页面的访问。唯一的异常(exception)是登陆页面和注销确认页面。 要将访问限制为仅登录用户,我在 web.config 中有以下内容:
使用 .Net 框架,Web Api。 我的 Controller 上有 [Authorize] 属性,某些端点上有 [AllowAnonymous]。 我有几个允许匿名的端点,但如果用户通过身份验证
当我应用这段代码时授权 Controller 方法和 AllowAnonymous在里面的一种操作方法上,所有操作方法都将被授权,除了上面有 AllowAnonymous 元数据的方法。显然,操作方法
我有一个 Asp.net MVC 应用程序,我在其中使用 Azure AD 身份验证对用户进行身份验证。我想允许用户在不登录的情况下访问一些 api Controller 。我尝试将 [AllowAn
在我的 .NET MVC 4 中,我添加了一个全局过滤器以保护我的 Controller 。 这是使用以下方法完成的: public static void RegisterGlobalFilters
这让我困惑了一段时间。显然,常见的类似情况似乎都不适用。我可能错过了一些明显的东西,但我看不到它。 在我的 Mvc Web 应用程序中,我使用 Authorize 和 AllowAnonymous 属
我有两个 MVC 5 应用程序,据我所知,它们都是相同的(一个是从另一个创建的)。然而,其中一个 AllowAnonymous 不起作用。 如果我在 FilterConfig 中注册 filters.
我正在使用 Web API Controller 构建 Web 服务。但是,当我尝试使用 [allowanonymous] 属性装饰我的任何操作时,它不起作用......这是一张图片 我只是想知道这里
当我创建一个新的 asp.net mvc 4.0 应用程序时,我做的第一件事 就是创建并设置自定义授权全局过滤器,如下所示: //FilterConfig.cs public static void
我通过继承 DelegatingHandler 实现了基于 JWT 的身份验证并将类添加为 configuration.MessageHandlers.Add(new MyDelegatingHand
我想使用 AllowAnonymous 和自定义 AuthenticationFilter。有人可以指出我使用 AllowAnonymous 或其他替代方法的正确方向吗?谢谢 我创建了自己的自定义过滤
我是一名优秀的程序员,十分优秀!