【问题标题】:Asp.net Proper Authorization MessageAsp.net 正确授权消息
【发布时间】:2011-04-06 15:13:55
【问题描述】:

这些是我在web.config中的设置:

<location path="Salary.aspx"> 
    <system.web> 
        <authorization>
            <allow roles="Salary Admin" /> 
            <deny users="*"/> 
        </authorization>
    </system.web> 
</location>

web.config 中,这工作得很好,但是,我想向用户显示一条适当的消息,说明他没有被授权,并提供一个返回链接。相反,它会直接进入登录表单,我该如何解决这个问题?任何帮助将不胜感激。

btn登录代码

protected void btnLogin_Click(object sender, EventArgs e)
        {
            bool bCheckUser;
            try
            {
               if ((txtUserName.Text == "") || (txtPassword.Text == ""))
                {
                    lblError.Visible = true;
                    lblError.ForeColor = System.Drawing.Color.Red;
                    lblError.Text = "Enter UserName and Password";
                }


                    {
                        bCheckUser = Membership.ValidateUser(txtUserName.Text, txtPassword.Text);
                        FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true);
                          FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text);
                        if (bCheckUser == true)
                        {
                            lblError.Visible = false;
                            Response.Redirect("MainMenu.aspx");
                        }
                        else
                        {
                            lblError.Visible = true;
                            lblError.ForeColor = System.Drawing.Color.Red;
                            lblError.Text = "You Username or Password is Invalid. Please try Again";
                        }

                }

            }
            catch(Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }

加载中

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Page.User.Identity.IsAuthenticated && !string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))                   
                    Response.Redirect("Unauthorized.aspx");
            }

        }

每次它给 Page.User.Identity.IsAuthenticated 为假,如果我在开始时从一个页面跳转到另一个页面,它给真,然后它返回假。

【问题讨论】:

  • 我得到了类似 Stackoverflow 本身的这种形式.. protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { base.HandleUnauthorizedRequest(filterContext); } else { filterContext.Result = new ViewResult { ViewName = "AccessDenied" }; } } 但它也不太好用
  • 重定向到登录页面时为什么不把这些信息放到登录页面上呢?
  • 检查这个:weblogs.asp.net/gurusarkar/archive/2010/01/12/… 仅供参考:使用编辑向您的问题添加详细信息,而不是将其添加为评论。
  • 是的,我看到了..它也不起作用
  • 我猜你说的是asp.net mvc?

标签: asp.net web-config forms-authentication


【解决方案1】:

最好在你的表中创建一个字段,如 user_type 并修改验证登录部分的函数

   if(user_type.ToString()=="admin")
       {
             //do other authentication stuff
       }
   else
       {
             Response.Redirect("urerrorpage.aspx");
       }

【讨论】:

    【解决方案2】:
    <location path="Salary.aspx"> 
        <system.web> 
            <authorization>
                <allow roles="Salary,Admin" /> 
                <deny users="*"/> 
            </authorization>
        </system.web> 
    </location>
    there should be comma in allow roles tag
    

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 2013-10-02
      • 2017-06-08
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 2019-05-16
      • 2016-11-02
      • 1970-01-01
      相关资源
      最近更新 更多