如何检查用户是否已登录 [英] How to check if user is logged in

查看:76
本文介绍了如何检查用户是否已登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个登录页面,用户必须在其中提供用户名和密码才能访问某些特定资源,他们可以在其中上传图像,或者只是编辑一些关于自己的描述.我的web.config文件如下所示:

I have created a Login page where users must provide username and password to have access to some specific resources, where they can upload images, or just edit some description about themselves. My web.config file looks like this:

    <authentication mode="Forms">
      <forms loginUrl="Secure/Login.aspx" defaultUrl="index.aspx" name=".ASPXFORMSAUTH" timeout="30"/>
    </authentication>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>

<location path="Secure">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>

因此,当用户键入用户名和pw时,他将被重定向到index.aspx页面.取决于用户是否登录,index.aspx应该显示或隐藏一些内容.这是我检查他是否登录的方式:

So when the user has typed in the username and pw, he is redirected to the index.aspx page. Depending wether the user is logged in or not, the index.aspx should show or hide some stuff. This is how I check if he is logged in:

bool isLoggedIn = HttpContext.Current.User.Identity.IsAuthenticated;  
    if (isLoggedIn) 
    {
        placeHolder2.Visible = true;
        ...
    }

现在的问题是:HttpContext.Current.User.Identity.IsAuthenticated;始终返回true,因此未经授权的人员将看到应该隐藏的内容.

Now the problem is that the: HttpContext.Current.User.Identity.IsAuthenticated; ALWAYS returns true, so unauthorised people will be seeing the stuff that should be hidden.

我不确定:HttpContext.Current.User.Identity.IsAuthenticated;我只是在Google上搜索如何检查用户是否已登录",建议为:HttpContext.Current.User.Identity.IsAuthenticated;

I am not sure about the: HttpContext.Current.User.Identity.IsAuthenticated; I just googled "How to check if user is logged in", and suggestions were the: HttpContext.Current.User.Identity.IsAuthenticated;

我只希望已登录的人员查看私人资料.我该怎么办?如何使:HttpContext.Current.User.Identity.IsAuthenticated仅在用户登录时返回true?谢谢

I want only the people that are logged in to view the private stuff. How do I go about this? How do I make the: HttpContext.Current.User.Identity.IsAuthenticated only return true when the user is logged in? Thanks

推荐答案

if (Request.IsAuthenticated) {.....}

根据一些评论进行编辑通过表格"进行身份验证,请点击此处

edit based on some comments Authenticated via "Forms", check here

HttpContext.Current.User.Identity.IsAuthenticated //将为表单"//协商"正在使用集成的Windows//等

HttpContext.Current.User.Identity.IsAuthenticated // will be "Forms" if using forms based auth // "Negotiate" is using windows integrated // etc

如果使用.net 4.5,并且您想要"SET"用户声明.建议阅读 ClaimsPrincipal

If using .net 4.5 and you wanted "SET" user claims. The ClaimsPrincipal is recommended reading

这篇关于如何检查用户是否已登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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