ASP.Net身份验证如果ReturnURL null UserData为空

时间:2011-05-15 09:20:29

标签: asp.net authentication asp.net-authentication returnurl

您好我的登录页面有问题 情景是,
例如,我访问www.mydomain.com/admin/,它将我重定向到具有ReturnURL参数的登录页面。 www.mydomain.com/login.aspx?ReturnURL=%2fAdmin%2f。
我正在使用管理员帐户登录,一切正常 但是如果我直接去Login.aspx这意味着没有ReturnURL QueryString字段 我使用相同的管理员帐户登录,但是当我登录后将其重新定向到登录页面时,我尝试访问www.mydomain.com/admin/。

我正在做这样的导航。我错过了什么?

//The code block that is logging in admin.
//check if there is a ReturnURL
if (QueryStringTool.IsExistAndNotNull("ReturnURL"))
{
    Session["UserType"] = UserTypes.UserType.Admin;
    Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserName.Text.Trim(), false));
}
//ReturnURL doesn't exists.
else
{
    FormsAuthentication.SetAuthCookie(txtUserName.Text, cbUserRememberMe.Checked);
    Response.Redirect("/Admin/Default.aspx");
}

2 个答案:

答案 0 :(得分:1)

这是一个示例web.config

<configuration>
   <system.web>
   <authentication mode="Forms">
      <forms 
      name="401kApp" 
      loginUrl="/login.aspx"
      cookieless="AutoDetect"
      defaultUrl="myCustomLogin.aspx">
      <credentials passwordFormat = "SHA1">   
         <user name="UserName" 
         password="07B7F3EE06F278DB966BE960E7CBBD103DF30CA6"/>
      </credentials>   
      </forms>
   </authentication>
   </system.web>
</configuration><br/>

defaultUrl="yourdefaultpageURL"中设置web.config
OR
你可以使用FormsAuthentication.RedirectFromLoginPage Method (String, Boolean)

答案 1 :(得分:1)

现在尝试一下。替换您的代码

//check if there is a ReturnURL

    if (QueryStringTool.IsExistAndNotNull("ReturnURL"))
    {
        Session["UserType"] = UserTypes.UserType.Admin;
        Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserName.Text.Trim(), false));
    }
    //ReturnURL doesn't exists.
    else
    {
        FormsAuthentication.SetAuthCookie(txtUserName.Text, cbUserRememberMe.Checked);
        Response.Redirect("/Admin/Default.aspx");
    }

这一个

if("Check if User Is Authentic")
{
Session["UserType"] = UserTypes.UserType.Admin;
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, cbUserRememberMe.Checked);
}

这适用于您的整个代码。将经过身份验证的用户重定向回最初请求的URL或默认URL 如果用户为Default,请检查Load页面Session["UserType"]活动Admin,然后将其重定向到管理页面