gpt4 book ai didi

c# - InvalidOperationException : No authenticationScheme was specified, 并且没有找到 DefaultChallengeScheme

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

我们有一个 Net Core 2.1 API 项目。我们使用请求 header 来检索 API key ,我们根据数据库检查该 key 是否与预期 key 之一匹配。如果是,那么我们允许请求继续,否则我们要发回未经授权的响应。

我们的 startup.cs

services.AddAuthorization(options =>
{
options.AddPolicy("APIKeyAuth", policyCorrectUser =>
{
policyCorrectUser.Requirements.Add(new APIKeyAuthReq());
});

});
services.AddSingleton<Microsoft.AspNetCore.Authorization.IAuthorizationHandler, APIKeyAuthHandler>();

我们的 APIKeyAuthHandler.cs

public class APIKeyAuthReq : IAuthorizationRequirement { }

public class APIKeyAuthHandler : AuthorizationHandler<APIKeyAuthReq>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, APIKeyAuthReq requirement)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
if (requirement == null)
throw new ArgumentNullException(nameof(requirement));

var httpContext = context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext;

var headers = httpContext.HttpContext.Request.Headers;
if (headers.TryGetValue("Authorization", out Microsoft.Extensions.Primitives.StringValues value))
{
using (DBContext db = new DBContext ())
{
var token = value.First().Split(" ")[1];
var login = db.Login.FirstOrDefault(l => l.Apikey == token);
if (login == null)
{
context.Fail();
httpContext.HttpContext.Response.StatusCode = 403;
return Task.CompletedTask;
} else
{
httpContext.HttpContext.Items.Add("CurrentUser", login);
context.Succeed(requirement);
return Task.CompletedTask;
}
}
}
}
}

和我们的controller.cs

    [Route("api/[controller]/[action]")]
[Authorize("APIKeyAuth")]
[ApiController]
public class SomeController : ControllerBase
{
}

当有效 key 存在时一切正常,但当它不存在时,将抛出 500 内部错误,而不是 403,而不是 No authenticationScheme。

我们是 net core 的新手(来自 Net Framework/Forms Authentication),所以如果有更准确的方法来进行这种身份验证,请告诉我。

错误信息:

InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)

最佳答案

首选基于 token 的身份验证。但是,如果您确实需要自定义 ApiKeyAuth 方案,那么,这是可能的。

首先,Authorize("APIKeyAuth") 在这里似乎没有意义,因为我们必须在授权前对用户进行身份验证。当有传入请求时,服务器不知道用户是谁。因此,让我们将 ApiKeyAuthAuthorization 移至 Authentication

为此,只需创建一个可用于保存选项的虚拟 ApiKeyAuthOpts

public class ApiKeyAuthOpts : AuthenticationSchemeOptions
{
}

和一个简单的 ApiKeyAuthHandler 来处理身份验证(我只是复制了上面的一些代码):

public class ApiKeyAuthHandler : AuthenticationHandler<ApiKeyAuthOpts>
{
public ApiKeyAuthHandler(IOptionsMonitor<ApiKeyAuthOpts> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{
}

private const string API_TOKEN_PREFIX = "api-key";

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
string token = null;
string authorization = Request.Headers["Authorization"];

if (string.IsNullOrEmpty(authorization)) {
return AuthenticateResult.NoResult();
}

if (authorization.StartsWith(API_TOKEN_PREFIX, StringComparison.OrdinalIgnoreCase)) {
token = authorization.Substring(API_TOKEN_PREFIX.Length).Trim();
}

if (string.IsNullOrEmpty(token)) {
return AuthenticateResult.NoResult();
}

// does the token match ?
bool res =false;
using (DBContext db = new DBContext()) {
var login = db.Login.FirstOrDefault(l => l.Apikey == token); // query db
res = login ==null ? false : true ;
}

if (!res) {
return AuthenticateResult.Fail($"token {API_TOKEN_PREFIX} not match");
}
else {
var id=new ClaimsIdentity(
new Claim[] { new Claim("Key", token) }, // not safe , just as an example , should custom claims on your own
Scheme.Name
);
ClaimsPrincipal principal=new ClaimsPrincipal( id);
var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), Scheme.Name);
return AuthenticateResult.Success(ticket);
}
}
}

最后,我们还需要一些配置来让它们工作:

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddAuthentication("ApiKeyAuth")
.AddScheme<ApiKeyAuthOpts,ApiKeyAuthHandler>("ApiKeyAuth","ApiKeyAuth",opts=>{ });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseMvc();
}

当您向受 [Authorize] 保护的操作方法发送请求时:

GET https://localhost:44366/api/values/1 HTTP/1.1
Authorization: api-key xxx_yyy_zzz

响应将是 HTTP/1.1 200 OK。当您发送没有正确 key 的请求时,响应将是:

HTTP/1.1 401 Unauthorized
Server: Kestrel
X-SourceFiles: =?UTF-8?B?RDpccmVwb3J0XDIwMThcOVw5LTEyXFNPLkFwaUtleUF1dGhcQXBwXEFwcFxhcGlcdmFsdWVzXDE=?=
X-Powered-By: ASP.NET
Date: Wed, 12 Sep 2018 08:33:23 GMT
Content-Length: 0

关于c# - InvalidOperationException : No authenticationScheme was specified, 并且没有找到 DefaultChallengeScheme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52287542/

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