![](/img/trans.png)
[英]AuthorizeAttribute works for MVC Controller but doesn't for ApiController
[英]AuthorizeAttribute doesn't work when refresh the page
我有一些授權問題:
AuthorizeWebForm
代碼在加載頁面時起作用,我不在管理員組中,所以我沒有訪問權限,這很棒。 但是當我刷新頁面時,我可以訪問該頁面,並且代碼AuthorizeWebFormAttribute
在刷新時不會運行。 有什么解決辦法嗎?
[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;
}
}
非常感謝!
你在這里重新發明輪子。 有一個非常好的AuthorizeAttribute
class 您可以使用: https://docs.microsoft.com/en-us/dotnet/api/system.webasp=.mvc。
另請查看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.