身份验证&授权无法正常工作

时间:2013-05-15 07:51:51

标签: c# asp.net .net visual-studio-2010 authentication

我是第一次处理身份验证和授权。

在我的web.config中,我有以下代码进行身份验证&授权:

<authentication mode="Forms">
    <forms loginUrl="Authentication.aspx" timeout="30" defaultUrl="Default.aspx" cookieless="AutoDetect" >
        <credentials passwordFormat="Clear">
            <user name="shiv" password="abc@123"/>
            <user name="raj" password="abc@123"/>
        </credentials>


    </forms>

</authentication>
  <authorization>
      <deny users="?"/>
  </authorization>

并且

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

            </authorization>
        </system.web>

    </location>


    <location path="users.aspx">
        <system.web>
            <authorization>
                <allow users="shiv"/>
                <allow users="raj"/>
                <deny users="*"/>
            </authorization>
        </system.web>

    </location>

.cs代码:

protected void btnLogin_Click(object sender, EventArgs e)
        {
            if(FormsAuthentication.Authenticate(txtUserName.Text,txtPassword.Text))
            {
                FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,true);
            }
        }

根据我的期望,当我从'raj'用户登录时,它应该将我重定向到users.aspx,但每次将其重定向到Default.aspx

为什么这样工作呢?

我做错了吗?

请帮帮我。

1 个答案:

答案 0 :(得分:1)

在您的web.config中,在authentication/forms部分中,相应地更改defaultUrl

更改

defaultUrl="Default.aspx"

到此:

defaultUrl="users.aspx"

关于 FormsAuthentication.RedirectFromLoginPage

Redirects an authenticated user back to the originally requested URL 
or the default URL.
相关问题