Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC授权属性工作而不注销_C#_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# MVC授权属性工作而不注销

C# MVC授权属性工作而不注销,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我有一个控制器类,只有特定的Active Directory组才能访问该类 [Authorize(Roles = @"Domain\GroupName")] public class AdminToolsController : Controller { ... } 现在我正在测试。。我目前不在这个小组里。。但是如果我加上我自己。。我尝试访问此控制器中的任何内容,但仍被要求登录,而我的凭据不起作用。然而。。如果我加上我自己。。然后注销。。然后重新登录。。然后尝试访问此控制器中的任何内容

我有一个控制器类,只有特定的Active Directory组才能访问该类

[Authorize(Roles = @"Domain\GroupName")]
public class AdminToolsController : Controller
{
    ...
}
现在我正在测试。。我目前不在这个小组里。。但是如果我加上我自己。。我尝试访问此控制器中的任何内容,但仍被要求登录,而我的凭据不起作用。然而。。如果我加上我自己。。然后注销。。然后重新登录。。然后尝试访问此控制器中的任何内容,因为它可以识别我并允许我访问

是否有任何方法可以在瞬间完成?也就是说,我可以将自己添加到组中并成功访问控制器中的任何方法,而无需注销和重新登录吗

更新

我编辑了Camilo Terevin,以回答以下问题。他们的回答是出于某种原因。。无论何时我从特定组中添加或删除我自己。。该组不属于变量

以下是我的更新:

public class AuthorizeByActiveDirectoryGroups : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var roles = Roles.Split(',');
        using (var domainContext = new PrincipalContext(ContextType.Domain, "domainname"))
        {
            var user = httpContext.User.Identity.Name;
            using (var domainUser = UserPrincipal.FindByIdentity(domainContext, httpContext.User.Identity.Name))
            {
                var adgroup = GroupPrincipal.FindByIdentity(domainContext, "Domain\\GroupName");
                bool member = domainUser.IsMemberOf(adgroup);
                var groups = domainUser.GetAuthorizationGroups();
                return member;
            }
        }
    }
}

IIRC,当您使用默认Windows身份验证登录时,角色仅获得一次,因此为了始终获得最新的身份验证,您可以使用自定义属性。
由于这将始终检查AD值,因此您可以使用一些缓存,仅在需要时刷新值,但这取决于您的具体情况

注意:我现在没有VS,因此可能存在一些拼写问题

public class AuthorizeByActiveDirectoryGroupsAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var roles = Roles.Split(",");
        using (var domainContext = new PrincipalContext(ContextType.Domain))
        {
            using (var domainUser = UserPrincipal.FindByIdentity(domainContext, httpContext.User.Identity.Name))
            {
                var groups = domainUser.GetAuthorizationGroups();
                return groups
                    .Select(x => x.Name) // the group name
                    .Any(x => roles.Contains(x)); // any group is one of the specified in the Roles property of the attribute
            }
        }
    }
}
所以你会像这样使用它:

[AuthorizeByActiveDirectoryGroups(Roles = "Group1,Group2")]
public ActionResult Index()
{
    return View();
}

在Windows中,如果用户被分配或从组中删除,此更改将从下次登录时生效。这是Windows的行为。因此,在您注销并登录之后,它会按照预期工作,正如您所看到的

当您双击Windows计算机中的某个组时,该组底部会显示此信息,表示更改在登录后生效


如果您正在使用cookie,则需要确保在分配新角色后使用新角色重新创建cookie

var userManager = HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager);
IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut("ApplicationCookie");
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, oAuthIdentity );
var userManager=HttpContext.GetOwinContext().GetUserManager();
ClaimsIdentity oAuthIdentity=等待用户.GenerateUserIdentityAsync(userManager);
IAAuthenticationManager authenticationManager=HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut(“ApplicationOkie”);
SignIn(新的AuthenticationProperties(){IsPersistent=false},oAuthIdentity);

在向用户添加角色“Domain\GroupName”后立即添加此代码有一种方法:创建自定义授权属性并使用
System.DirectoryServices.AccountManagement
@CamiloTerevinto注意将您的想法作为答案吗?导致问题的原因是登录用户的cookie。在您注销并重新登录之前,不会更改与帐户关联的声明again@Nkosi那么需要发生什么呢?@BviLLe_Kid取决于你想要什么行为。当我尝试此操作时,我在
override
AuthorizeCore
下看到一条红色曲线,说
找不到合适的方法来重写
Nvmd,明白了。。Visual Studio自动添加了
System.Web.Http
而不是
System.Web.Mvc
我已经将自己添加到广告组中,并且我仍然会得到一个用于登录的弹出窗口,以及何时登录。。它仍然在说我没有访问权限好吧,所以我添加了我自己,并让它工作了。。但是现在当我退出并尝试访问这些方法时。。我还是被允许的?好吧,老实说,我知道问题是什么,但现在当我加上我自己。。它不起作用了。。这只有在我注销后才能生效,我知道情况就是这样。。但老实说。。为什么MVC甚至提供了一个安全协议,就像我要问的那样,如果一个被删除的人在注销之前仍然可以访问一个只有授权用户才能访问的位置?