简体   繁体   English

刷新页面时 AuthorizeAttribute 不起作用

[英]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.

相关问题 AuthorizeAttribute适用于MVC Controller,但不适用于ApiController - AuthorizeAttribute works for MVC Controller but doesn't for ApiController 使用 Polly 刷新令牌的 RestEase 不起作用 - RestEase with Polly refresh token doesn't work 自定义AuthorizeAttribute Ninject属性注入不起作用(注入属性具有需要注入的子依赖服务) - Custom AuthorizeAttribute Ninject Property Injection doesn't work (injected property have sub dependant services which need to be injected) 单击gridview按钮时页面未刷新 - Page doesn't refresh on gridview button click 绑定到纯数据表(没有后面的数据库)时,dataGridview1.refresh不起作用 - dataGridview1.refresh doesn't work when bound to a pure dataTable (no Database behind) 如果角色名称包含空格,则无法使AuthorizeAttribute有效 - Can't make AuthorizeAttribute work, if role name contains spaces 自定义AuthorizeAttribute-端点不起作用 - Custom AuthorizeAttribute - Endpoint not work jQuery Click-在第1页加载时不起作用,但是在重新加载页面时起作用 - jQuery Click - Doesn't work when page 1st loaded, but works if page is reloaded Windows Phone 8 SDK不会刷新页面的XAML部分 - Windows Phone 8 SDK doesn't refresh XAML part of a page 更新命令在特定页面上不起作用 - Update Command doesn't work on a specific page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM