第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

來自數(shù)據(jù)庫表的 ASP.NET Core 2 授權(quán)

來自數(shù)據(jù)庫表的 ASP.NET Core 2 授權(quán)

C#
慕慕森 2022-06-12 14:44:27
我有一系列網(wǎng)頁,這些網(wǎng)頁的授權(quán)是在自定義數(shù)據(jù)庫表中定義的。例如,我有一個名為“超級用戶”的角色,該角色允許訪問某些網(wǎng)頁。我有分配給該角色的用戶。我不明白如何將 Authorize 屬性放在控制器上并傳入頁面名稱(視圖),然后從我的數(shù)據(jù)庫中讀取某種類型的自定義處理程序以查看用戶是否在具有權(quán)限的組中. 我一直在這里閱讀基于策略的授權(quán):https ://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-2.2并試圖理解它我的情況。我是否在基于策略的授權(quán)的正確軌道上,或者在允許用戶訪問頁面之前是否有另一種方法來進(jìn)行數(shù)據(jù)庫檢查權(quán)限?
查看完整描述

2 回答

?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗 獲得超6個贊

Authorize屬性本身僅用于指定您在特定頁面或控制器上所需的授權(quán)類型。此屬性旨在與身份框架一起使用,并且可以包括角色、策略和身份驗證方案。

您需要在 Identity 框架和您的數(shù)據(jù)庫之間建立一座橋梁,這可以通過自定義UserStoreand來完成RoleStore,這在此頁面上有詳細(xì)描述。

總結(jié)一個相當(dāng)復(fù)雜的過程:

  1. Authorize屬性指示瀏覽器對您的用戶進(jìn)行身份驗證

  2. 您的用戶被重定向到身份驗證頁面

  3. 如果成功,您將獲得一個ClaimsPrincipal實例,然后您需要通過自定義映射到您的數(shù)據(jù)庫用戶UserStore

  4. 然后可以根據(jù)數(shù)據(jù)庫角色檢查您的用戶

這是所有這些實際操作的簡短示例(不完全完整,因為它的代碼太多了)。

啟動.cs

// This class is what allows you to use [Authorize(Roles="Role")] and check the roles with the custom logic implemented in the user store (by default, roles are checked against the ClaimsPrincipal roles claims)

public class CustomRoleChecker : AuthorizationHandler<RolesAuthorizationRequirement>

{

    private readonly UserManager<User> _userManager;


    public CustomRoleChecker(UserManager<User> userManager)

    {

        _userManager = userManager;

    }


    protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, RolesAuthorizationRequirement requirement)

    {

        var user = await _userManager.GetUserAsync(context.User);


        // for simplicity, I use only one role at a time in the attribute

        var singleRole = requirement.AllowedRoles.Single();

        if (await _userManager.IsInRoleAsync(user, singleRole))

            context.Succeed(requirement);

    }

}


public void ConfigureServices(IServiceCollection services)

{

    services

    .AddIdentity<User, Role>()

    .AddUserStore<MyUserStore>()

    .AddRoleStore<MyRoleStore>();


    // custom role checks, to check the roles in DB 

   services.AddScoped<IAuthorizationHandler, CustomRoleChecker>();

}

您的 EF Core 實體在哪里User,是哪里。Role


我的用戶商店


public class MyUserStore : IUserStore<User>, IUserRoleStore<User>, IQueryableUserStore<User>

{

    private Context _db;

    private RoleManager<Role> _roleManager;

   ...


    public async Task<User> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)

    {

        // bridge your ClaimsPrincipal to your DB users

        var user = db.Users.SingleOrDefault(_ => _.Email.ToUpper() == normalizedUserName);

        return await Task.FromResult(user);

    }


   ...

    public async Task<bool> IsInRoleAsync(User user, string roleName, CancellationToken cancellationToken)

    {

        if (roleName == null)

            return true;


        // your custom logic to check role in DB

        var result = user.Roles.Any(_ => _.RoleName == roleName);

        return await Task.FromResult(result);

    }


查看完整回答
反對 回復(fù) 2022-06-12
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗 獲得超11個贊

.Net Core -> 如果要使用基于策略的方法,則必須在 startup.cs 的 ConfigureServices 方法中定義策略定義


例子:


 services.AddAuthorization(options =>

            {

                options.AddPolicy("UserPolicy", policy => policy.RequireRole("USER"));

            });

然后您可以在控制器或操作方法中應(yīng)用如下策略。


授權(quán)(策略 = “用戶策略”)


查看完整回答
反對 回復(fù) 2022-06-12
  • 2 回答
  • 0 關(guān)注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號