gpt4 book ai didi

c# - 没有指定 authenticationScheme,也没有找到基于自定义策略的授权的 DefaultForbidScheme

转载 作者:太空狗 更新时间:2023-10-30 00:29:38 57 4
gpt4 key购买 nike

我有一个自定义的基于策略的授权处理程序,定义如下。身份验证是在用户点击此应用程序之前处理的,因此我只需要授权。我收到错误:

No authenticationScheme was specified, and there was no DefaultForbidScheme.

如果授权检查成功,那么我就不会收到错误,一切都很好。只有在授权检查失败时才会发生此错误。我希望在失败时返回 401。

public class EasRequirement : IAuthorizationRequirement
{
public EasRequirement(string easBaseAddress, string applicationName, bool bypassAuthorization)
{
_client = GetConfiguredClient(easBaseAddress);
_applicationName = applicationName;
_bypassAuthorization = bypassAuthorization;
}

public async Task<bool> IsAuthorized(ActionContext actionContext)
{
...
}
}
public class EasHandler : AuthorizationHandler<EasRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, EasRequirement requirement)
{
var mvcContext = context.Resource as ActionContext;

bool isAuthorized;

try
{
isAuthorized = requirement.IsAuthorized(mvcContext).Result;
}
catch (Exception)
{
// TODO: log the error?
isAuthorized = false;
}

if (isAuthorized)
{
context.Succeed(requirement);
return Task.CompletedTask;
}

context.Fail();
return Task.FromResult(0);
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var easBaseAddress = Configuration.GetSection("EasBaseAddress").Value;
var applicationName = Configuration.GetSection("ApplicationName").Value;
var bypassAuthorization = bool.Parse(Configuration.GetSection("BypassEasAuthorization").Value);

var policy = new AuthorizationPolicyBuilder()
.AddRequirements(new EasRequirement(easBaseAddress, applicationName, bypassAuthorization))
.Build();

services.AddAuthorization(options =>
{
options.AddPolicy("EAS", policy);
});

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

services.AddSingleton<IAuthorizationHandler, EasHandler>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseAuthentication();

app.UseMvc();
}
}

最佳答案

授权和身份验证在 ASP.NET Core 中紧密相关。当授权失败时,这将传递给身份验证处理程序以处理授权失败。

因此,即使您不需要实际身份验证来识别您的用户,您仍然需要设置一些能够处理禁止和质询结果(403 和 401)的身份验证方案。

为此,您需要调用 AddAuthentication() 并配置默认的禁止/质询方案:

services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = "scheme name";

// you can also skip this to make the challenge scheme handle the forbid as well
options.DefaultForbidScheme = "scheme name";

// of course you also need to register that scheme, e.g. using
options.AddScheme<MySchemeHandler>("scheme name", "scheme display name");
});

MySchemeHandler 需要实现 IAuthenticationHandler,在您的情况下,您尤其需要实现 ChallengeAsyncForbidAsync:

public class MySchemeHandler : IAuthenticationHandler
{
private HttpContext _context;

public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
_context = context;
return Task.CompletedTask;
}

public Task<AuthenticateResult> AuthenticateAsync()
=> Task.FromResult(AuthenticateResult.NoResult());

public Task ChallengeAsync(AuthenticationProperties properties)
{
// do something
}

public Task ForbidAsync(AuthenticationProperties properties)
{
// do something
}
}

关于c# - 没有指定 authenticationScheme,也没有找到基于自定义策略的授权的 DefaultForbidScheme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142845/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com