小编max*_*ini的帖子

ASP.NET Core 2.0将Cookie和承载授权结合在一起,用于同一端点

我在VS17中使用"Web应用程序(模型 - 视图 - 控制器)"模板和".Net Framework"+"ASP.NET Core 2"作为配置创建了一个新的ASP.NET核心Web应用程序项目.身份验证配置设置为"个人用户帐户".

我有以下示例端点:

[Produces("application/json")]
[Route("api/price")]
[Authorize(Roles = "PriceViwer", AuthenticationSchemes = "Cookies,Bearer")]
public class PriceController : Controller
{

    public IActionResult Get()
    {
        return Ok(new Dictionary<string, string> { {"Galleon/Pound",
                                                   "999.999" } );
    }
}
Run Code Online (Sandbox Code Playgroud)

"Cookies,Bearer"是通过连接CookieAuthenticationDefaults.AuthenticationScheme和派生而得到的JwtBearerDefaults.AuthenticationScheme.

目标是能够为端点配置授权,以便可以使用令牌和cookie身份验证方法访问它.

以下是我在Startup.cs中进行身份验证的设置:

    services.AddAuthentication()
        .AddCookie(cfg => { cfg.SlidingExpiration = true;})
        .AddJwtBearer(cfg => {
            cfg.RequireHttpsMetadata = false;
            cfg.SaveToken = true;
            cfg.TokenValidationParameters = new TokenValidationParameters() {
                                                    ValidIssuer = Configuration["Tokens:Issuer"],
                                                    ValidAudience = Configuration["Tokens:Issuer"],
                                                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
                                                };
        });
Run Code Online (Sandbox Code Playgroud)

因此,当我尝试使用浏览器访问端点时,我得到了一个带有空白html页面的401响应. …

.net c# asp.net authentication asp.net-core-2.0

19
推荐指数
3
解决办法
7761
查看次数

标签 统计

.net ×1

asp.net ×1

asp.net-core-2.0 ×1

authentication ×1

c# ×1