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
如何为ASP.NET MVC站点实现安全性以拒绝对特定组的访问?_Asp.net_Asp.net Mvc_Asp.net Mvc 2_Asp.net Mvc Routing - Fatal编程技术网

如何为ASP.NET MVC站点实现安全性以拒绝对特定组的访问?

如何为ASP.NET MVC站点实现安全性以拒绝对特定组的访问?,asp.net,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-routing,Asp.net,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc Routing,我有一个内部公司ASP.NET MVC网站 要求(1):当任何人在网络上时,除了一个广告组(例如:广告销售组)外,他们都可以访问此网站 要求(2):例如,如果有访问权限的人将url(例如:)传递给销售组人员,他仍然不应该访问,并且需要显示一条自定义消息,说明“您无权查看此页面” 如果该场景类似于向一个广告组发出访问权,并拒绝所有其他广告组的访问权,那么这是公平的。它可以从IIS完成。我想知道怎么做 有人为这个场景实施了安全措施吗 我感谢你的时间和回复 谢谢我相信这会对你有用 2个步骤。。。 你需

我有一个内部公司ASP.NET MVC网站

要求(1):当任何人在网络上时,除了一个广告组(例如:广告销售组)外,他们都可以访问此网站

要求(2):例如,如果有访问权限的人将url(例如:)传递给销售组人员,他仍然不应该访问,并且需要显示一条自定义消息,说明“您无权查看此页面”

如果该场景类似于向一个广告组发出访问权,并拒绝所有其他广告组的访问权,那么这是公平的。它可以从IIS完成。我想知道怎么做

有人为这个场景实施了安全措施吗

我感谢你的时间和回复


谢谢

我相信这会对你有用

2个步骤。。。 你需要做的第一件事是在你的Global.asax.cs中尝试

protected void Application_AcquireRequestState(Object sender, EventArgs e)
    {
        //Context.Handler in this state, we can access Session.
        if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
        {
            //Is it a session created in this request?
            if (Session.IsNewSession)
            {
                //Am I already authenticated?
                if (User.Identity.IsAuthenticated)
                {
                    //if already authenticated, check if it is logon, if not, we just logout,
                    //else, we can continue the logon and reset the user identity.
                    string url = Request.Url.ToString();
                    if (url.IndexOf("Account/LogOn") < 0)
                    {
                        FormsAuthentication.SignOut();
                        Response.Redirect(Request.RawUrl);
                    }
                }
            }
            else
            {
                //Am I already authenticated?
                if (User.Identity.IsAuthenticated)
                {
                    try
                    {
                        /// Here we try to get the current role of the user logged in from the session 
                        SessionUser myRole = CurrentUser.GetRole();
                        string[] strRole;
                        switch (myRole)
                        {
                            case Role.ADSales:
                                {
                                    string[] Roles = { "ADSales" };
                                    strRole = Roles;
                                }
                                break;
                            case Role.DeptHead:
                                {
                                    string[] Roles = { "DeptHead" };
                                    strRole = Roles;
                                }
                                break;
                            case Role.ProductionCrew:
                                {
                                    string[] Roles = { "ProductionCrew" };
                                    strRole = Roles;
                                }
                                break;
                            case Role.Admin:
                                {
                                    string[] Roles = { "Admin" };
                                    strRole = Roles;
                                }
                                break;
                            default:
                                throw new AuthenticationException(ErrorEnum.Impossible);
                            //break;
                        }
                        Context.User = new System.Security.Principal.GenericPrincipal(Context.User.Identity, strRole); 

                    }
                    catch (Exception)
                    {
                        string url = Request.Url.ToString();
                        if (url.IndexOf("Account/LogOn") < 0)
                        {
                            FormsAuthentication.SignOut();
                            Response.Redirect(Request.RawUrl);
                        }
                    }


                }
            }
        }
    }
请注意,我没有在角色中包含ADSales,这意味着具有所述角色的用户无法访问页面字母


希望这有帮助。如果它对你有帮助,请投票,如果它解决了你的问题,请不要忘记将其标记为答案。谢谢

您需要在应用程序目录上启用Windows身份验证。然后更改相关文件/目录的ACL,以拒绝对特定组的访问。最后,将IIS 403错误映射到您的拒绝访问方法。

一件简单的事情:这是在网络上,Windows身份验证用于识别用户。非表单身份验证。所以没有会话的概念
[Authorize(Roles = "ProductionCrew,DeptHead,Admin")]   
public ActionResult Letter()
{
   Return View();
}