首页 文章

在ASP.Net MVC 4中向ASP.Net Web窗体添加身份验证

提问于
浏览
1

我创建了一个MVC应用程序 . 我在每个控制器上创建了身份验证,它可以运行 . 如果我不是授权用户,我会被重定向到登录页面 . 我对控制器的授权(sitemapnode角色)没有任何问题 .

现在,我在ASP.Net MVC项目中创建了一个ASP.NET Web窗体 . 我在网络表单上放了一个reportviewer . 我在MVC上创建了一个View,将asp.net web表单放在iFrame标签中,这也有效 . 当我呼叫正确的控制器时,我可以查看reportviewer .

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

如何在我的网络表单上申请授权?与MVC授权类似 . 如果我不是授权用户(让我们说'管理员'),我必须被重定向到登录页面,否则我必须无法访问网络表单 . 我怎么做?

2 回答

  • 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")]
    
  • 0

    使用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
        }
    

相关问题