gpt4 book ai didi

asp.net - 如何在不对其进行硬编码的情况下使用角色 (asp.net)?

转载 作者:行者123 更新时间:2023-12-04 18:50:14 25 4
gpt4 key购买 nike

创建/删除角色时,我不想修改代码。

if (HttpContext.Current.User.IsInRole("Super Admin") ||
HttpContext.Current.User.IsInRole("Admin") ||
HttpContext.Current.User.IsInRole("Support"))
{
if (HttpContext.Current.User.IsInRole("Admin"))
{
ListBox1.DataSource = Roles.GetAllRoles().Except(
new[] { "Super Admin" });

}
if (HttpContext.Current.User.IsInRole("Support"))
{
ListBox1.DataSource = Roles.GetAllRoles().Except(
new[] { "Super Admin", "Admin" });
}
fillDropDownCustomers();
}

最佳答案

将这些值放在静态类中:

public static class MyRoles
{
public const string Admin = "Admin";
public const string SuperAdmin = "Super Admin";
public const string Support = "Support";
}

现在你可以像这样重用它们:
if (HttpContext.Current.User.IsInRole(MyRoles.SuperAdmin) ||
HttpContext.Current.User.IsInRole(MyRoles.Admin) ||
HttpContext.Current.User.IsInRole(MyRoles.Support))
{

关于asp.net - 如何在不对其进行硬编码的情况下使用角色 (asp.net)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com