检查cookie是否设置

发布于 2025-01-08 01:44:33 字数 1709 浏览 0 评论 0原文

我正在尝试在用户屏幕顶部呈现一个工具栏(如果他们已登录),他们可以在其中访问他们的帐户等。如果他们未登录,它将显示一个允许他们登录的表单。我没有完全确定如何使用 C# 和 Razor 执行此操作,并不断遇到语法和编译错误。

我当前的形式如下:我有一个文件,_siteLayout.cshtml。这会将工具栏存储在屏幕顶部。它根据外部数据库检查登录表单,如果通过身份验证,则会创建给客户端一个 cookie。我本质上想要的形式很

if(user logged in)
   render account management page
else{
   render login page
}

简单,但我遇到了很多问题。这是我的代码,到目前为止,删除了大部分内容:

<body>
    @using System.Text;
    @using System.Net.Sockets;
   @{
        if(Request.Cookies["mpUsername"] == null){
        //if user is not logged in  
            //some authentication is ran, if passed, isValid is set to true    
                if (isValid) {
                    //login is valid, set cookie
                    HttpCookie cookie = Request.Cookies.Get("mpUsername");
                if(cookie == null) {
                    cookie = new HttpCookie("mpUsername");
                    cookie.Value = username;
                    cookie.Expires = DateTime.Now.AddDays(3);
                    Response.Cookies.Add(cookie);
                }
                } else {
                    //login invalid, prompt for pass again
                <text>Password incorrect, please try again</text>
                }
            } 
    }
  }//end of razor, HTML begins
    <html>
        <body>
        @{  //if cookie is set
            //if(Request.Cookies["mpUsername"] == null){   

        }
        <h2>ACCOUNT MANAGEMENT</h2>
        @{
          } else {//user not logged in, cookie not set
         }
        //login form
        </body>
    </html>

做我想做的事情的最佳方法是什么?在我的实际代码中,登录表单和帐户管理页面显然要大得多,因此有点令人困惑,因此我从上面的代码中删除了它们。

I am trying to render a toolbar at the top of a users screen if they are logged in, where they can access their account etc. If they are not logged in, it will display a form allowing them to log in. I'm not entirely sure how to do this using C# and Razor and keep hitting syntax and compilation errors.

My current form is as follows : I have one file, _siteLayout.cshtml. This stores the toolbar at the top of the screen. It checks the login form against the external database and if it is authenticated, creates gives the client a cookie. The form I essentially want is

if(user logged in)
   render account management page
else{
   render login page
}

Simple, but I am running into a lot of problems. Here is my code, removing the bulk, so far :

<body>
    @using System.Text;
    @using System.Net.Sockets;
   @{
        if(Request.Cookies["mpUsername"] == null){
        //if user is not logged in  
            //some authentication is ran, if passed, isValid is set to true    
                if (isValid) {
                    //login is valid, set cookie
                    HttpCookie cookie = Request.Cookies.Get("mpUsername");
                if(cookie == null) {
                    cookie = new HttpCookie("mpUsername");
                    cookie.Value = username;
                    cookie.Expires = DateTime.Now.AddDays(3);
                    Response.Cookies.Add(cookie);
                }
                } else {
                    //login invalid, prompt for pass again
                <text>Password incorrect, please try again</text>
                }
            } 
    }
  }//end of razor, HTML begins
    <html>
        <body>
        @{  //if cookie is set
            //if(Request.Cookies["mpUsername"] == null){   

        }
        <h2>ACCOUNT MANAGEMENT</h2>
        @{
          } else {//user not logged in, cookie not set
         }
        //login form
        </body>
    </html>

What is the best way of doing what I wish to do? In my actual code, the login form and account management pages are obviously far larger, so it's a little more confusing, hence why I removed these from the code above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文