【问题标题】:Add authentication to ASP.Net Web Forms inside ASP.Net MVC 4在 ASP.Net MVC 4 中向 ASP.Net Web 窗体添加身份验证
【发布时间】:2014-01-23 13:09:57
【问题描述】:

我创建了一个 MVC 应用程序。我在每个控制器上创建了身份验证,并且它可以工作。如果我不是授权用户,我将被重定向到登录页面。我对控制器的授权(站点地图节点角色)没有任何问题。

现在,我在我的 ASP.Net MVC 项目中创建了一个 ASP.NET Web 表单。我在网络表单上放了一个reportviewer。我在 MVC 上创建了一个视图,将 asp.net Web 表单放在 iFrame 标记内,这也可以。当我调用正确的控制器时,我可以查看报告查看器。

但是,如果我没有通过简单地键入 ASP.NET Web 窗体的位置来获得授权,我仍然可以查看或访问 ASP.NET Web 窗体(使用 reportviewer)。

如何在我的网络表单上应用授权?类似于 MVC 上的授权。如果我不是授权用户(比如说“管理员”),我必须被重定向到登录页面,否则我不能访问网络表单。我怎么做?

【问题讨论】:

标签: c# asp.net asp.net-mvc authorization


【解决方案1】:

更大的问题是为什么您需要混合 MVC 和 WebForms 但无论如何...

MS 文档可能会是您最大的帮助:

http://www.asp.net/web-forms/tutorials/security/roles/role-based-authorization-cs

您可以在 web.config 中锁定类似于:

  <location path="YourPage.aspx">    
      <system.web>    
           <authorization>    
               <allow roles="sitemapnode" /> 
           </authorization>    
      </system.web>    
 </location>

或者在具有属性的页面方法级别:

[PrincipalPermission(SecurityAction.Demand, Role = "sitemapnode")]

【讨论】:

  • ahhh 因为在 MVC 中我不能使用 ReportViewer,这就是为什么我使用 web 表单作为 reportviewer,然后在 MVC 中的 cshtml 中使用 iframe 标签,这样我就可以在 MVC 视图中的 iframe 标签内查看 reportviewer
  • 使用为这种类型的全局验证而制作的 MVC 过滤器。
【解决方案2】:

使用 MVC 过滤器:

    using System;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using System.Web.Security;
    using PortalAPI.SPModels;
    using SICommon.Enums;
    using SICommon.LoggingOperations;

    namespace SupplierPortal.Security {
        public class AuthorizedUser : AuthorizeAttribute {
            public bool IsAuthorized { get; set; }

            protected override bool AuthorizeCore(HttpContextBase httpContext) {

                if (Authenticated())
                  return this.IsAuthorized = true;
            }

            protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {
                if (filterContext.HttpContext.Request.IsAjaxRequest()) {
                    filterContext.HttpContext.Response.StatusCode = 403;
                    filterContext.Result = new JsonResult {
                        Data = new {
                            Error = "SessionTimeOut"
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                    filterContext.HttpContext.Response.End();
                } else {
                    filterContext.Result = new RedirectToRouteResult(
                        new RouteValueDictionary(
                            new {
                                controller = "Account",
                                action = "Login"
                            }
                        )
                    );
                }
                base.HandleUnauthorizedRequest(filterContext);
            }
        }
    }

    [AuthorizedUser(IsAuthorized = true)]
    public class myformclass(){
        //some code in here for form
    }

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 2014-06-02
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2021-02-27
    • 2017-08-13
    相关资源
    最近更新 更多