在我的mvc5项目中禁用未授权用户的操作链接我确实喜欢这个
@if (User.IsInRole("Admin") | User.IsInRole("Manager"))
{
@Html.ActionLink("Add New Record", "ProductTypeIndex", "ProductType")
}
Run Code Online (Sandbox Code Playgroud)
但如果要检查的角色很多,那么@if()会变长.怎么避免这个?我是否需要定制助手(如果是这样,我该如何处理它)?帮助赞赏..
我有一个MVC 3应用程序,使用Windows身份验证和声明使用WIF 4.5.
通过AD组中的成员资格(当前)控制对应用程序的访问:
<deny users="?" />
<allow roles="domain\somegroup" />
<deny users="*" />
Run Code Online (Sandbox Code Playgroud)
除AD组外,我们还有需要添加的自定义角色.(此应用程序正在从Forms转换为Windows身份验证)
为了支持这些自定义角色(直到它们在AD中管理),我们将它们添加为ClaimTypes.GroupSid声明给用户,以便现有代码利用[Authorize("ADMIN")]
并User.IsInRole("ADMIN")
继续运行:
Application_PostAuthenticateRequest(object sender, EventArgs e)
{
var identity = ClaimsPrincipal.Current.Identity as WindowsIdentity;
var roles = userDAL.GetRoles(identity.Name);
foreach(var role in roles)
{
identity.AddClaim(new Claim(ClaimTypes.GroupSid, role));
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都按预期工作.
除非当前用户不是某个自定义角色(如ADMIN)的成员,并且该角色在AD中也不存在
我们使用[Authorize("ADMIN")]
Controller Action Methods,以及各种User.IsInRole("ADMIN")
场景中的依赖实例.在那些发生错误并且应用程序爆炸的情况下.
AD基础架构正处于升级/迁移过程中.我不知道那里的所有细节,但我知道有一些域名,据说他们之间有信任,基础设施人员已经提到这些信任关系正在运行.
所以我想我想知道两件事:
这似乎不是我们的代码应该处理的东西.那么域名真的可能出现什么问题?我可以找出信任关系失败的"可信"域名吗?
解决这个问题的最佳方法是什么?我不喜欢编写辅助方法和Authorize()
子类只是为了捕获这个异常.
我正在使用WindowsPrincipal的IsInRole方法来检查WPF和Winforms应用程序中的组成员身份.我正在生成一个身份令牌,可以用于任何AD用户(不一定是实际登录到计算机的用户 - 取决于我正在做什么我不一定要进行身份验证,我只使用基本的信息级令牌(我认为它的正确名称是"身份令牌").
第一次在特定计算机上运行此代码时,操作系统会为指定的用户生成标识令牌.然后,IsInRole函数使用该标记来验证组成员身份.它很快,所以我非常喜欢它.但是,后续调用创建WindowsIdentity/WindowsPrincipal会引用现有令牌而不是创建新令牌.我知道如何更新令牌的唯一方法是退出计算机或重新启动(清除令牌缓存).有谁知道重置缓存身份令牌的更好方法?
示例代码C#:
Using System.Security.Principal;
WindowsIdentity impersonationLevelIdentity = new WindowsIdentity("Some_UserID_That_Isn't_Me", null);
WindowsPrincipal identityWindowsPrincipal = new WindowsPrincipal(impersonationLevelIdentity);
If (identityWindowsPrincipal.IsInRole("AN_AD_GROUP")) { ...
Run Code Online (Sandbox Code Playgroud)
VB:
Imports System.Security.Principal
Dim impersonationLevelIdentity = New WindowsIdentity("Some_UserID_That_Isn't_Me", Nothing)
Dim identityWindowsPrincipal = New WindowsPrincipal(impersonationLevelIdentity)
if identityWindowsPrincipal.IsInRole("AN_AD_GROUP") then...
Run Code Online (Sandbox Code Playgroud) 有人知道如何WindowsPrincipal.IsInRole("domain\role")
使用活动目录通用组吗?
假设当前用户是名为Domain的域中名为Role的组的成员,并且Role组是Active Directory中的Global组.然后,以下代码将产生result = true:
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool result = wp.IsInRole(@"domain\Role");
Run Code Online (Sandbox Code Playgroud)
但是,如果将Role组更改为通用组,则代码将生成result = false.
我已经为我的MVC4应用程序创建了一个自定义角色提供程序,我已成功覆盖CreateRole,GetAllRoles和RoleExists方法,并将它们链接到我现有的数据库,如下所示:
namespace Project.Providers
{
public class MyProvider : System.Web.Security.SqlRoleProvider
{
private MyContext dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
private Repository<MyUser> userRepository;
private Repository<Role> roleRepository;
public MyProvider()
{
this.userRepository = new Repository<MyUser>(dbcontext);
this.roleRepository = new Repository<Role>(dbcontext);
}
public override string[] GetAllRoles()
{
IEnumerable<Role> dbRoles = roleRepository.GetAll();
int dbRolesCount = roleRepository.GetAll().Count();
string[] roles = new string[dbRolesCount];
int i = 0;
foreach(var role in dbRoles)
{
roles[i] = role.Name;
i++;
}
return roles;
}
public override bool RoleExists(string roleName)
{
string[] roles = { "Admin", …
Run Code Online (Sandbox Code Playgroud) 我试图在完全"登录"到我正在构建的站点之前检查给定用户是否有角色.通常我会使用代码:
User.IsInRole("CustomRole")
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,该行总是产生"假" - 我相信这是因为使用User.IsInRole,用户必须已经完全登录.我试图在我的帐户控制器的LogOn方法中检查这条信息,所以用户尚未登录(我认为).
我将如何返回用户对象,以便我可以执行下面尝试执行的操作:
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
// Need to identify the user because the "User" is not officially 'logged in' yet and cannot be accessed via "User.IsInRole" - am I correct in this understanding
MembershipUser u = Membership.GetUser(model.UserName);
if (u.IsInRole("Administrator"))
. . . . truncated
Run Code Online (Sandbox Code Playgroud)
上面的代码抛出以下错误:
'System.Web.Security.MembershipUser' does not contain a definition for 'IsInRole' and no extension method 'IsInRole' accepting a first argument of …
Run Code Online (Sandbox Code Playgroud)