开发者

Asp.net Proper Authorization Message

T开发者_如何学Gohese are my settings in web.config:

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

In web.config this is working perfectly fine, but, I want to display an appropriate message to the user that he is not authorized and give a link to go back. Instead it's going to the login form directly, how can I resolve this? Any help will be appreciated.

btnlogin code

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();
            }
        }

on load

  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");
            }

        }

everytime it is giving Page.User.Identity.IsAuthenticated as false if i jump form one page to other at start it is giving true then it returns false.


better create a field in ur table like user_type and modify the function which is authenticating the login part as

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


<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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜