Aspnet Core - web.config授权
创始人
2024-09-20 05:00:25
0

在ASP.NET Core中,web.config文件不再用于配置授权规则。相反,授权规则是在Startup类的ConfigureServices方法中配置的。

以下是一个示例,演示如何在ASP.NET Core中配置授权规则:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        services.AddAuthorization(options =>
        {
            options.AddPolicy("RequireAdminRole", policy =>
                policy.RequireRole("Admin"));
            
            options.AddPolicy("RequireUserRole", policy =>
                policy.RequireRole("User"));
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // ...

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        // ...

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

在上面的示例中,我们在ConfigureServices方法中使用AddAuthorization方法配置了两个授权策略。其中,"RequireAdminRole"策略要求用户必须具有"Admin"角色,"RequireUserRole"策略要求用户必须具有"User"角色。

在Configure方法中,我们使用了UseAuthentication和UseAuthorization中间件来启用授权。这样,当请求到达时,将会自动应用授权规则。

请注意,上述示例是ASP.NET Core的通用方法,不依赖于web.config文件。

相关内容

热门资讯

透视透视挂!aapoker辅助... 透视透视挂!aapoker辅助工具存在吗,wepoker透视最简单三个步骤,2025版教程(一直是有...
透视黑科技!wepoker透视... 透视黑科技!wepoker透视脚本安卓,佛手大菠萝辅助,透明挂教程(原来有挂);亲,其实确实真的有挂...
透视科技!wepoker辅助器... 透视科技!wepoker辅助器安装包,德普之星透视免费,AI教程(真是真的有挂);大神普及一款德州a...
透视插件!aapoker透视脚... 透视插件!aapoker透视脚本下载,werplan外卦神器,2025新版教程(一向是真的有挂)1、...
透视计算!aapoker透视脚... 透视计算!aapoker透视脚本,拱趴大菠萝万能辅助器,透明教程(果然真的是有挂);玩家必备必赢加哟...
透视插件!wpk有脚本吗,we... 透视插件!wpk有脚本吗,wepoker私人局外卦,曝光教程(一直是有挂)1、每一步都需要思考,不同...
透视真的!wepoker脚本,... 透视真的!wepoker脚本,we poker辅助器下载,微扑克教程(从前是有挂)1、玩家可以在we...
透视黑科技!wepoker透视... 透视黑科技!wepoker透视脚本苹果版,we-poker有人玩吗,2025新版教程(确实真的是有挂...
透视教学!aapoker免费透... 透视教学!aapoker免费透视脚本,hhpoker软件靠谱吗,攻略教程(原本有挂);亲,其实确实真...
透视ai代打!wpk辅助ai,... 透视ai代打!wpk辅助ai,we-poker辅助,力荐教程(真是是有挂);玩家必备必赢加哟《136...