C# 如何在基于表单的身份验证中检查用户是否登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10780313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:14:08  来源:igfitidea点击:

How to check if user is logged in or not in forms based authentication

c#asp.netasp.net-membershipforms-authentication

提问by Raghuveer

I want to implement forms based authentication manually in my website.

我想在我的网站中手动实施基于表单的身份验证。

I am using Web.configfile for data store

我正在使用Web.config文件进行数据存储

<authentication mode="Forms">
  <forms loginUrl="~/Login.aspx" 
         name=".ASPXAUTH"
         path="/"
         requireSSL="false"
         slidingExpiration="true"
         defaultUrl="~/Admin/OrderHistory.aspx"
         cookieless="UseDeviceProfile"
         enableCrossAppRedirects="false"
         >
    <credentials passwordFormat="Clear">
      <user name="Admin" password="adm123$"/>
      <user name="Administrator" password="adm234%"/>
    </credentials>
  </forms>
</authentication>
<authorization>
  <deny users ="?" />
  <allow users = "*" />
</authorization>

There is a Login.aspxpage at root level in that im using ASP.NET login control to get username and password.

Login.aspx在根级别有一个页面,我使用 ASP.NET 登录控件来获取用户名和密码。

Everything works fine but when the user is logged inand manually go to login.aspxpage, its not redirect the user to defaultUrl page.

一切正常,但是当用户logged in手动转到login.aspx页面时,它不会将用户重定向到 defaultUrl 页面。

I want to redirect the user to a specific page/defaultUrl page, if he is logged in and came manually to login.aspx page

我想将用户重定向到特定页面/defaultUrl 页面,如果他已登录并手动进入 login.aspx 页面

How to do it?

怎么做?

Login Button-Click

登录按钮-单击

if (FormsAuthentication.Authenticate(LoginUser.UserName, LoginUser.Password))
    {
        FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);

    }

采纳答案by Raab

    if(HttpContext.Current.User.Identity.IsAuthenticated)
    {

    //Redirect to Default page
    Response.Redirect("default.aspx");
    }

回答by Sanket

I generally use the strategy of session for this.

我通常为此使用会话策略。

If a session is established then redirect the user to a specific page or else direct him to login.

如果建立了会话,则将用户重定向到特定页面或引导他登录。

You can refer for more info here http://asp.net-tutorials.com/state/sessions/

您可以在此处参考更多信息http://asp.net-tutorials.com/state/sessions/

回答by Sai

use the forms attribute defaulturl="xyz.aspx"(your URL) in your web.config file

在 web.config 文件中使用表单属性 defaulturl="xyz.aspx"(您的 URL)

回答by ASP-Developer

 protected void Page_Load(object sender, EventArgs e)
    {  
        if (!Request.UrlReferrer.Equals("http://localhost:1360/login_page.aspx"))
        {
            Response.Redirect("ERROR ==>? 404");
        }
    }