我正在实现一个自定义IPrincipal
,我想在多个应用程序中使用它。关于IsInRole
方法,我有两个问题。。。
1) 是否建议我使用带有自定义IPrincipal
的自定义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);
}
我想说,你可以自由地使用任何你想要的技术,以确定用途是否在某个角色中。RoleProvider在这里不是必须的。
不能将应用程序名称作为构造函数参数传递,然后将其存储在成员中吗?
1)您不必有角色提供程序,但是如果您有,您的自定义角色提供程序应该继承自System.Web.Security.RoleProvider,这样它就可以在不破坏应用程序的情况下与任何其他角色提供程序交换
2) web.config或app.config是这个的合适位置