Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查用户是否已登录?_C#_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 如何检查用户是否已登录?

C# 如何检查用户是否已登录?,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,是否有更好的方法来检查用户是否已登录?您可以始终使用user.Identity.i验证进行检查,如下所示 try { string signedInUserID = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; //do some stuff if the user is singed in } Catch { //do some stuff if the user is singed in }

是否有更好的方法来检查用户是否已登录?

您可以始终使用
user.Identity.i验证
进行检查,如下所示

try
{
    string signedInUserID = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
    //do some stuff if the user is singed in
}
Catch
{
    //do some stuff if the user is singed in
}
注意
声明
主要用于存储附加数据,更多用于
授权
目的。

可能的重复:
    if(User.Identity.IsAuthenticated)
    {
        //Authenticated 
    }
    else
    {
        //Not Authenticated
    }