Custom Policy-Based Authorization in ASP.NET Framework

时光毁灭记忆、已成空白 提交于 2021-02-10 17:21:31

问题


I'm using ASP.NET API with .NET Framework 4.7.1

And I'm looking for custom policy-based authorization. This document described custom policy-based authorization for .NET core, but how could I implement it for .NET framework?


回答1:


There is a Nuget package that backports the Policy-based authorization to .NET Framework 4.

You can use Microsoft.Owin.Security.Authorization.Mvc or Microsoft.Owin.Security.Authorization.WebApi package, depending on what you need. Both packages will bring you the same functionalities described in the document you linked.

E.g.:

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"));
            });
        }

    }
}

Source: https://vladimirgeorgiev.com/blog/policy-based-authorization-in-asp-net-4/



来源:https://stackoverflow.com/questions/55797250/custom-policy-based-authorization-in-asp-net-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!