【发布时间】:2019-04-22 15:59:55
【问题描述】:
我正在使用带有 .NET Framework 4.7.1 的 ASP.NET API
我正在寻找基于策略的自定义授权。 This 文档描述了 .NET 核心的自定义基于策略的授权,但我如何才能为 .NET 框架实现它?
【问题讨论】:
标签: c# asp.net asp.net-web-api authorization
我正在使用带有 .NET Framework 4.7.1 的 ASP.NET API
我正在寻找基于策略的自定义授权。 This 文档描述了 .NET 核心的自定义基于策略的授权,但我如何才能为 .NET 框架实现它?
【问题讨论】:
标签: c# asp.net asp.net-web-api authorization
有一个 Nuget 包将基于策略的授权反向移植到 .NET Framework 4。
您可以根据需要使用Microsoft.Owin.Security.Authorization.Mvc 或Microsoft.Owin.Security.Authorization.WebApi 包。这两个包都将为您带来您链接的文档中描述的相同功能。
例如:
using Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Authorization.Infrastructure;
using System.IdentityModel.Claims;
[assembly: OwinStartup(typeof(Startup))]
namespace Concep.Platform.WebApi.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseAuthorization(options =>
{
options.AddPolicy("AbleToCreateUser", policy => policy.RequireClaim(JwtClaimTypes.Role, "Manager"));
});
}
}
}
来源: https://vladimirgeorgiev.com/blog/policy-based-authorization-in-asp-net-4/
【讨论】: