FormsAuthentication.SignOut方法和重定向 [英] FormsAuthentication.SignOut Method and redirect

查看:68
本文介绍了FormsAuthentication.SignOut方法和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想将用户重定向到他们刚刚访问的页面.
如果他们正在访问的页面不允许匿名用户,则
FormsAuthenticationModule将自动将用户重定向到登录名
页面.

我使用了此代码,但是在所有情况下,它始终会将用户重定向到登录"页面.

Hi guys,

I want to redirects the user to the page they were just visiting.
If the page they were just visiting does not allow anonymous users, then the
FormsAuthenticationModule will automatically redirect the user to the login
page.

I used this code but it always redirects user to Login page in all cases.

FormsAuthentication.SignOut();

FormsAuthentication.RedirectToLoginPage();



然后,我该怎么办?



Then, what can i do please ?

推荐答案

像strogg所说的那样,请使用Response.Redirect("http://your.url.here/", true);.如果匿名用户无法访问该页面,则无论如何都将重定向到登录页面.

另外请注意,以下方法名称是否具有足够的描述性?

Like strogg said, use Response.Redirect("http://your.url.here/", true); instead. If the page is not accessible to anonymous users, the redirect to the login page will happen anyway.

on another note, isn''t the following method name descriptive enough?

FormsAuthentication.RedirectToLoginPage();


尝试一下.
try this .
FormsAuthentication.RedirectFromLoginPage();


根据我对问题的理解:您希望应用程序记住需要身份验证的页面.如果用户未经身份验证访问该页面,则该应用程序将重定向到登录页面,并且在登录后,它将自动重定向到该用户来自的区域.在这种情况下,您可以执行以下操作.

示例:

1. MyReports.aspx需要身份验证.
2.用户访问MyReports.aspx 未经身份验证

在MyReports.aspx的页面加载中,输入以下代码:

From what I understand from the question: you want the application to remember the authentication required page. If a user visits that page without authentication, the application redirects to the login page and after login, it redirects automatically to the area where the user is coming from. If this is the case, you can do the following.

Example:

1. MyReports.aspx requires authentication.
2. User visits MyReports.aspx without authentication

In the page load for MyReports.aspx, put the following code:

<br />
    if (!IsPostBack)<br />
    {<br />
        try<br />
        {<br />
           string tempUserName = Session["UserName"].ToString();<br />
        }<br />
        catch(Exception ex)<br />
        {<br />
           Session["LastVisitedURL"] = Request.Url.ToString();<br />
           Response.Redirect("LoginPage.aspx");<br />
        }<br />
<br />
    }<br />



在LoginPage.aspx身份验证按钮上,单击事件",然后输入以下代码



On the LoginPage.aspx Authenticate Button Click Event put the following code

<br />
    Button Click Event<br />
    {<br />
        //Validate user<br />
        if Validated<br />
        {<br />
             try<br />
             {<br />
                 string URLRedirect = Session["LastVisitedURL"].ToString();<br />
                 Response.Redirect(URLRedirect);<br />
             } <br />
             catch(Exception ex)<br />
             {<br />
                 Response.Redirect("Dashboard or landing page after authentication");<br />
             }<br />
        }<br />
    }<br />



很抱歉,如果不是这种情况.

-Nayan



Sorry if this is not the case for you.

-Nayan


这篇关于FormsAuthentication.SignOut方法和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆