国产精品久久久久久2021,日韩精品无码av中文无码版,亚洲精品久久久午夜麻豆,无码成人精品日本动漫纯h

010-68421378
當前您所在的位置:首頁>新聞中心>新品發(fā)布

Telerik :如何在ASP.NET Core中創(chuàng)建自定義AuthorizeAttribute?

發(fā)布時間:2021/04/30 瀏覽量:3532
Telerik UI for ASP.NET Core是用于跨平臺響應式Web和云開發(fā)的最完整的UI工具集

Telerik UI for ASP.NET Core是用于跨平臺響應式Web和云開發(fā)的最完整的UI工具集,擁有超過60個由Kendo UI支持的ASP.NET核心組件。它的響應式和自適應的HTML5網(wǎng)格,提供從過濾、排序數(shù)據(jù)到分頁和分層數(shù)據(jù)分組等100多項高級功能。

 

 

ASP.NET Core MVC中,使用AuthorizeAttribute類執(zhí)行授權。 這是一個使用基于角色授權的簡單示例:

[Authorize(Roles ="Admin,Moderator")]

public class AdminController : Controller

{

// ...

}

具有AdminModerator角色的用戶將有權訪問AdminController中的操作。

Admin,Moderator”字符串解決錯字的好方法,因此讓我們將角色名稱移動到一組字符串常量中:

public static class RoleConstants

{

public const string Admin ="Admin";

public const string Moderator ="Moderator";

// more roles

}

AdminController現(xiàn)在變?yōu)椋?/span>

[Authorize(Roles=RoleConstants.Admin+","+RoleConstants.Moderator)]

public class AdminController : Controller

{

// ...

}

無法將插值字符串如$ {RoleConstants.Admin},{RoleConstants.Moderator}”與屬性一起使用。

這是一個很好的示例,其中擴展了AuthorizeAttribute類是有意義的。 由于我們試圖簡化在控制器或操作上調(diào)用基于角色的授權過程,因此創(chuàng)建一個自定義AuthorizeByRoleAttribute

///

 

 

/// Specifies that the class or method thatthis attribute is applied to requires role-based authorization.

 

/// To authorize users with either role Aor role B, use:

 

///

 

/// [AuthorizeByRole("A","B")]

 

///

 

/// To only authorize users with both roleA and role B, use:

 

///

 

/// [AuthorizeByRole("A")]

 

/// [AuthorizeByRole("B")]

 

///

 

///

 

 

public class AuthorizeByRoleAttribute :AuthorizeAttribute

 

{

 

public MyAuthorizeAttribute(params string[]roles)

 

{

 

Roles = String.Join(",", roles);

 

}

 

}

 

可以如下來使用它:

 

[AuthorizeByRole(RoleConstants.Admin,RoleConstants.Moderator)]

 

public class AdminController : Controller

 

{

 

// ...

 

}

 

 

 

下一篇:Splashtop : 教育機構的安全遠程訪問,遠程支持和協(xié)作解決方案
上一篇:Kofax Power PDF:如何編輯PDF文檔?

                               

 京ICP備09015132號-996 | 違法和不良信息舉報電話:4006561155

                                   © Copyright 2000-2026 北京哲想軟件有限公司版權所有 | 地址:北京市海淀區(qū)西三環(huán)北路50號豪柏大廈C2座11層1105室

                         北京哲想軟件集團旗下網(wǎng)站:哲想軟件 | 哲想動畫

                            華滋生物