繁体   English   中英

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

[英]ASP.NET Core Web API Skip Authentication

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

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

认识某人,我该如何通过某些特定的操作来跳过身份验证?

更新的答案:您应该尝试根据框架编写代码。 示例中的中间件不适用于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>(); 使用此代码,您可以使用标准属性Autorize限制任何控制器:

[Authorize(ActiveAuthenticationSchemes = "Basic")]
[Route("api/[controller]")]
public class ValuesController : Controller

并在应用程序级别应用授权过滤器时使用属性AllowAnonymous

原始答案: 来自文档

[AllowAnonymous]添加到家庭控制器,以便匿名用户在注册之前可以获得有关该站点的信息。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM