2014-01-06 64 views
1

我创建了一个MVC应用程序。我在每个控制器上创建了身份验证,并且工作正常如果我不是授权用户,我会重定向到登录页面。控制器的授权(sitemapnode角色)没有问题。将验证添加到ASP.Net MVC内的ASP.Net Web窗体4

现在,我在ASP.Net MVC项目中创建了一个ASP.NET Web窗体。我在网络表单上放置了一个reportviewer。我在MVC上创建了一个视图,把asp.net web窗体放在了iFrame标签中,这也是可行的。当我打电话给正确的控制器时,我可以查看报告查看器。

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

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

+0

我想在你的web.config你应该添加一些代码http://www.asp.net/web-forms/tutorials/security/roles/role- based-authorization-cs – chenZ

+0

ahh是的,timothyclifford答案帮助 – TheMartianGuy

回答

1

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

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

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

可以在网络锁定的.config类似于:

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

或在带属性的页面方法级别:

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

ahhh,因为在MVC中我不能使用ReportViewer,这就是为什么我使用webview的形式为reportviewer,然后在MVC中使用iframe标签在cshtml,所以我可以查看iframe标签内的reportviewer MVC视图 – TheMartianGuy

+0

使用MVC过滤器进行这种类型的全局验证。 – JEuvin

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 
    }