gpt4 book ai didi

c# - ASP.NET Core Web API 跳过身份验证

转载 作者:行者123 更新时间:2023-12-01 09:47:55 27 4
gpt4 key购买 nike

我正在根据以下示例使用自定义基本身份验证编写 ASP.NET Core Web 应用程序:
ASP.NET Core Web API Authentication
现在我在我的用户 Controller 中有一个操作来注册用户。因此,我想将此方法传递给我的自定义属性“SkipAuthAttribute”,以便说,如果有人调用此方法(操作),您必须跳过身份验证(要注册的用户没有登录)。

但是 HttpContext 类型没有 ActionDescriptor 来获取 Action 的自定义属性

认识的人,我怎么能通过一些特定的 Action 跳过认证?

最佳答案

更新答案:
您应该尝试根据框架编写代码。示例中的中间件不适合 ASP.NET Core MVC。这是我的例子:

public class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticationOptions>
{
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var authHeader = (string)this.Request.Headers["Authorization"];

if (!string.IsNullOrEmpty(authHeader) && authHeader.StartsWith("basic", StringComparison.OrdinalIgnoreCase))
{
//Extract credentials
string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));

int seperatorIndex = usernamePassword.IndexOf(':');

var username = usernamePassword.Substring(0, seperatorIndex);
var password = usernamePassword.Substring(seperatorIndex + 1);

if (username == "test" && password == "test")
{
var user = new GenericPrincipal(new GenericIdentity("User"), null);
var ticket = new AuthenticationTicket(user, new AuthenticationProperties(), Options.AuthenticationScheme);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}

return Task.FromResult(AuthenticateResult.Fail("No valid user."));
}
}

public class BasicAuthenticationMiddleware : AuthenticationMiddleware<BasicAuthenticationOptions>
{
public BasicAuthenticationMiddleware(
RequestDelegate next,
IOptions<BasicAuthenticationOptions> options,
ILoggerFactory loggerFactory,
UrlEncoder encoder)
: base(next, options, loggerFactory, encoder)
{
}

protected override AuthenticationHandler<BasicAuthenticationOptions> CreateHandler()
{
return new BasicAuthenticationHandler();
}
}

public class BasicAuthenticationOptions : AuthenticationOptions
{
public BasicAuthenticationOptions()
{
AuthenticationScheme = "Basic";
AutomaticAuthenticate = true;
}
}

在 Startup.cs 注册 - app.UseMiddleware<BasicAuthenticationMiddleware>(); .使用此代码,您可以限制任何具有标准属性的 Controller Autorize :
[Authorize(ActiveAuthenticationSchemes = "Basic")]
[Route("api/[controller]")]
public class ValuesController : Controller

并使用属性 AllowAnonymous如果您在应用程序级别应用授权过滤器。

原答案:
From documentation

Add [AllowAnonymous] to the home controller so anonymous users can get information about the site before they register.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace ContactManager.Controllers {
[AllowAnonymous]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}

关于c# - ASP.NET Core Web API 跳过身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44677283/

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