[英]AuthorizeAttribute doesn't work when refresh the page
I have some problem with authorization:我有一些授权问题:
The AuthorizeWebForm
codes work when loading the page, I am not in the admin group so I don't have the access, which is great. AuthorizeWebForm
代码在加载页面时起作用,我不在管理员组中,所以我没有访问权限,这很棒。 But when I refresh the page, I have access to the page, and the code AuthorizeWebFormAttribute
doesn't run when refresh.但是当我刷新页面时,我可以访问该页面,并且代码
AuthorizeWebFormAttribute
在刷新时不会运行。 Any solution to this?有什么解决办法吗?
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeWebFormAttribute : System.Attribute
{
public AuthorizeWebFormAttribute(string Roles = null)
{
IPrincipal user = HttpContext.Current.User;
if (user.Identity.IsAuthenticated)
{
if (Roles == null)
return;
if (user.IsInRole("admin"))
return;
string[] roleArray = Roles.Split(',');
foreach (var role in roleArray)
{
if (user.IsInRole(role))
return;
}
}
HttpContext.Current.Server.TransferRequest("~/Unauthorized", false);
}
}
namespace Crew
{
[AuthorizeWebForm("admin")]
protected void Page_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Session["EmpNo_User"].ToString()))
{
Response.Redirect("~/ErrorPage.aspx?CustError=This page expired. Please close the broswer and open again.");
}
Page.MaintainScrollPositionOnPostBack = true;
}
} }
Thank you very much!非常感谢!
You are reinventing the wheel here.你在这里重新发明轮子。 There's a perfectly good
AuthorizeAttribute
class you can use: https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.authorizeattribute?view=aspnet-mvc-5.2有一个非常好的
AuthorizeAttribute
class 您可以使用: https://docs.microsoft.com/en-us/dotnet/api/system.webasp=.mvc。
Also take a look at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-security/roles/role-based-authorization-cs for role-based security in general.另请查看https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-security/roles/role-based-authorization-cs ,了解一般基于角色的安全性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.