简体   繁体   English

认证后,页面重定向回登录页面

[英]After authentication,page redirects back to login page

I have a simple login page as below: 我有一个简单的登录页面,如下所示:

   protected void btnLogIn_Click(object sender, EventArgs e)
{
    FormsAuthentication.Initialize();

    string CS = ConfigurationManager.ConnectionStrings["IS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
      {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select Roles from tblRegisteredUsers where Email=@Email and Password=@Password",con);
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
        SqlDataReader rdr = cmd.ExecuteReader();
        if(rdr.Read())
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                txtEmail.Text,
                DateTime.Now,
                DateTime.Now.AddMinutes(30),
                true,
                rdr.GetString(0),
                FormsAuthentication.FormsCookiePath);
            string hash = FormsAuthentication.Encrypt(ticket);

            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,hash);
            if(ticket.IsPersistent) cookie.Expires=ticket.Expiration;
            Response.Cookies.Add(cookie);

            string returnUrl = Request.QueryString["returnUrl"];
            if(returnUrl==null) returnUrl="/";
            Response.Redirect(returnUrl);
        }
        else
        {
            lblMessage.Text = "Invalid Email/Password.Please try again.";
        }
        rdr.Close();
    }
}

I have used folder level roles authentication with each folder having its own web.config file.I am sure that the user gets authenticated successfully because the see the username in loginName.This is definitely a redirection issue.What can i do to redirect the users to my Homepage after successful login? 我已经使用了文件夹级角色身份验证,每个文件夹都有自己的web.config文件。我确定用户已成功通过身份验证,因为在loginName中看到了用户名。这绝对是重定向问题。我应该怎么做才能重定向用户成功登录后返回我的主页?

Is anybody still helping me on this? 有人还在帮助我吗?

just a suggestion. 只是一个建议。 you may redirect to homepage or if required then create a global variable and maintain the last visited page url in it. 您可以重定向到主页,或者如果需要,则创建一个全局变量并在其中维护最后访问的页面URL。 hope it helps 希望能帮助到你

string returnUrl = Request.QueryString["returnUrl"]; 字符串returnUrl = Request.QueryString [“ returnUrl”];

I think your query string fetch null value thats why it statifises the IF condition and take the return URL as '/' so it redirects to current page. 我认为您的查询字符串获取null值,这就是为什么它规定IF条件并将返回URL用作'/'的原因,因此它重定向到当前页面。

Put a debug point near the highlighted portion and see what value you are getting for return url 将调试点放在突出显示的部分附近,并查看返回URL获得的值

Instead of 代替

string returnUrl = Request.QueryString["returnUrl"];
        if(returnUrl==null) returnUrl="/";
        Response.Redirect(returnUrl);

just write 写吧

 Response.Redirect(<"your url for the homepage">);

Eg 例如

For MVC application it will be Response.Redirect("/home/index"); 对于MVC应用程序,它将为Response.Redirect("/home/index");

You will have to redirect to another page like this: 您将必须重定向到另一个页面,如下所示:

Response.Redirect("myhomepage.aspx");

you need to provide your page name with .aspx extension at the end unlike that in MVC where you needed to provide the URL. 您需要在页面末尾提供.aspx扩展名,这与MVC中需要提供URL的扩展名不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 针对活动目录进行身份验证后,登录页面重定向到登录页面 - Login Page redirects to login page after authentication against active directory 除非应用程序池被回收,否则表单身份验证将重定向回登录页面 - Forms authentication redirects back to login page unless the app pool is recycled 团队:个人选项卡 =&gt; 成功的身份验证重定向回登录页面(由于未设置 cookie) - Teams: Personal Tab => successful authentication redirects back to login page (Due to cookie not being set) 页面重定向到登录页面 - Page redirects to login page 成功登录后返回需要身份验证的页面 - Go back to page that needed authentication after successful login 成功通过身份验证后,为什么我的应用程序返回登录页面? - Why is my application going back to the login page after successful authentication? 身份验证后,身份验证会重定向回Login.aspx - Forms authentication redirects back to Login.aspx after authenticating 在 IIS 中部署后,重新加载页面重定向到 angular 6 中的登录页面 - reload page redirects to login page in angular 6 after deploy in IIS 用户认证后访问登录页面 - User access to login page after authentication Challenge()始终重定向到登录页面 - Challenge() always redirects to Login Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM