小编MLE*_*MLE的帖子

在ASP.NET Core 1.1中配置基本身份验证中间件

我试图在ASP.NET Core 1.1应用程序中的MVC服务上配置基本身份验证.我想通过在服务操作上添加一个属性(而不是允许基本的auth应用程序范围)来指示服务需要Basic Authentincation.在做了一些阅读之后,似乎采用中间件过滤器的相应方法.

我在Middleware过滤器上找到的最全面的指南就在这里

上面的帖子表明我需要创建一个Pipeline类,如下所示

public class MyPipeline  
{
    public void Configure(IApplicationBuilder applicationBuilder) 
    {
        var options = // any additional configuration

        //I changed this to use the "UseMiddleware"
        applicationBuilder.UseMiddleware<AuthenticationMiddleware>(options);
    }
}
Run Code Online (Sandbox Code Playgroud)

我还需要一个中间件类.我从这里修改了例子

public class AuthenticationMiddleware
{
    private readonly RequestDelegate _next;

    public AuthenticationMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        string authHeader = context.Request.Headers["Authorization"];
        if (authHeader != null && authHeader.StartsWith("Basic"))
        {
            //Extract credentials
            string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
            Encoding encoding = Encoding.GetEncoding("iso-8859-1"); …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-core

6
推荐指数
1
解决办法
5381
查看次数

标签 统计

asp.net-core ×1

asp.net-mvc ×1