2012-07-25 67 views
3

我正在实现我想跨多个应用程序使用自定义IPrincipal。 我有关于IsInRole方法的2个问题...自定义IPrincipal与应用程序名称IsInRole

1)是否建议我使用自定义的的RoleProvider?我总是可以将检查用户角色的逻辑放在从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); 
} 

回答

2

我想说你可以自由地使用任何你想要的技术来确定一个使用是否在角色中。角色提供者在这里不是必须的。

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

+0

适当的地方,我想我会尽量避免使用自定义角色提供程序即可。检查用户是否是角色是我真正需要的唯一功能。传入应用程序名称听起来不错...谢谢:) – woggles 2012-07-25 09:30:20

1

1)您不必拥有角色提供者,但是如果您这样做,则想法是您的自定义角色提供者应该从System.Web.Security.RoleProvider继承,以便它可以交换为任何其他角色不打破应用

2)的web.config或app.config中提供商对于该