2023-02-16 326
我很熟悉ASP.NET安全模型,其中一个人可以允许/拒绝访问Web.config中的用户的访问权限.
<system.web>
<authorization>
<allow roles = "Admin" />
</authorization>
</system.web>
但是,我想要做的是给管理员用户一组可以检查的权限,例如,可以检查.具有”可以打印文档”的权限的管理员用户,”可以删除文档”
是出于盒子中可能的这些东西,或者我需要走上自定义路线吗?
您可以使用此MSDN文章.
但有许多我不喜欢Azman的东西,所以我把自己作为lease的补充(管理映射权限的额外表,API和管理工具).
我的自定义实现非常简单:
m-n在角色和权限之间的关系.
API”HASPERMISSION”,测试给定的主体是否具有给定的许可.这简单地迭代所有角色并检查角色是否具有给定权限.使用ASP.NET缓存缓存映射权限角色以出于性能原因.
它不开箱;但如果你想要更粒度,为什么不像”CAN打印”,”Candelete”,而不是像”管理员”一样的粒度角色?
如果它们想要一个容器类型方案,就像您在评论中指示您可以设置自定义IPrincipal – 在身份验证和使用每个新请求之后,您可以查看用户的角色成员资格(“管理员”,”公共”等. )然后在您的Iprincipal上覆盖Isinrole.您可以找到一个示例
我找到了 给出一个很好的例子
的文章
[Flags]
public enum Permissions
{
View = (1 << 0),
Add = (1 << 1),
Edit = (1 << 2),
Delete = (1 << 3),
Admin = (View | Add | Edit | Delete)
}
public ActionResult Authenticate(string username, string password)
{
var user = authenticationService.Authenticate(username, password);
Session["User"] = user;
return RedirectToAction("Somewhere", "Else");
}
public class PermissionsAttribute : ActionFilterAttribute
{
private readonly Permissions required;
public PermissionsAttribute(Permissions required)
{
this.required = required;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var user = filterContext.HttpContext.Session.GetUser();
if (user == null)
{
//send them off to the login page
var url = new UrlHelper(filterContext.RequestContext);
var loginUrl = url.Content("~/Home/Login");
filterContext.HttpContext.Response.Redirect(loginUrl, true);
}
else
{
if (!user.HasPermissions(required))
{
throw new AuthenticationException("You do not have the necessary permission to perform this action");
}
}
}
}
[Permissions(Permissions.View)]
public ActionResult Index()
{
// ...
}
以上所述是小编给大家介绍的ASP.NET安全角色和权限,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/33878.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态