【问题标题】:Custom IPrincipal with application name IsInRole具有应用程序名称 IsInRole 的自定义 IPrincipal
【发布时间】:2012-07-25 09:22:53
【问题描述】:

我正在实现一个自定义的IPrincipal,我想在多个应用程序中使用它。 我有两个关于IsInRole 方法的问题...

1) 是否建议我将自定义 RoleProvider 与自定义 IPrincipal 一起使用?我总是可以将检查用户角色的逻辑放在从 IPrincipal 继承的类中。

类似:

public class SSDSPrincipal : IPrincipal
{ 
    public SSDSPrincipal(SSDSIdentity identity)
    {
        this.Identity = identity;
    }        

    public IIdentity Identity {get;private set;}

    public bool IsInRole(string role)
    {   
        string[] roles = Roles.Providers["SSDSRoleProvider"].GetRolesForUser(Identity.Name);
        return roles.Any(s => role.Contains(s)); 
    }
}

2) 因为我想在多个 MVC3 应用程序中使用它。存储应用程序名称的最佳位置在哪里?我需要能够手动设置。

public bool IsInRole(string role)
{   
    string applicationName = [where can I store this globally for my asp.net mvc3 app]
    return AreTheyInARoleForThisApplication(applicationName, role);
}

【问题讨论】:

    标签: c# asp.net-mvc-3 roleprovider


    【解决方案1】:

    我想说你可以随意使用任何你想要的技术来确定一个用途是否在一个角色中。 RoleProvider 在这里不是必须的。

    您不能将应用程序名称作为构造函数参数传递,然后将其存储在成员中吗?

    【讨论】:

    • 我想我会避免使用自定义角色提供程序。检查用户是否在某个角色中是我真正需要的唯一功能。传递应用程序名称听起来不错...谢谢:)
    【解决方案2】:

    1) 您不必拥有角色提供者,但是如果您这样做了,那么您的自定义角色提供者应该继承自 System.Web.Security.RoleProvider 以便它可以与任何其他角色提供者互换不破坏应用程序

    2) web.config 或 app.config 是合适的地方

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多