C# 在ASP.NETMVC4中向ASP.NETWeb表单添加身份验证
我创建了一个MVC应用程序。我在每个控制器上都创建了身份验证,它可以正常工作。如果我不是授权用户,我将被重定向到登录页面。我对控制器的授权(sitemapnode角色)没有问题 现在,我在ASP.NETMVC项目中创建了一个ASP.NETWeb表单。我在web窗体上放置了一个reportviewer。我在MVC上创建了一个视图,将asp.NETWeb表单放在iFrame标记中,这也很有效。我可以在调用正确的控制器时查看reportviewer 但是,我仍然可以查看或访问ASP.NET Web表单(使用reportviewer),如果我没有通过简单键入ASP.NET Web表单的位置获得授权的话C# 在ASP.NETMVC4中向ASP.NETWeb表单添加身份验证,c#,asp.net,asp.net-mvc,authorization,C#,Asp.net,Asp.net Mvc,Authorization,我创建了一个MVC应用程序。我在每个控制器上都创建了身份验证,它可以正常工作。如果我不是授权用户,我将被重定向到登录页面。我对控制器的授权(sitemapnode角色)没有问题 现在,我在ASP.NETMVC项目中创建了一个ASP.NETWeb表单。我在web窗体上放置了一个reportviewer。我在MVC上创建了一个视图,将asp.NETWeb表单放在iFrame标记中,这也很有效。我可以在调用正确的控制器时查看reportviewer 但是,我仍然可以查看或访问ASP.NET Web表单
如何在web表单上应用授权?类似于MVC上的授权。如果我不是授权用户(比如“管理员”),我必须被重定向到登录页面,否则我不能访问web表单。我该怎么做 更大的问题是,为什么需要混合使用MVC和WebForms,但无论如何 MS文档可能是您最大的帮助: 您可以在web.config中锁定,类似于:
<location path="YourPage.aspx">
<system.web>
<authorization>
<allow roles="sitemapnode" />
</authorization>
</system.web>
</location>
使用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
}
我想你应该在web.config中添加一些代码啊是的,timothyclifford answer Helpedahh因为在MVC中我不能使用ReportViewer,这就是为什么我在ReportViewer中使用web表单,然后在MVC中的cshtml中使用iframe标记,这样我就可以在MVC视图中的iframe标记内查看ReportViewer使用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
}