Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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# 验证用户是否使用.Net登录_C#_Asp.net_.net_Authentication_.net 3.5 - Fatal编程技术网

C# 验证用户是否使用.Net登录

C# 验证用户是否使用.Net登录,c#,asp.net,.net,authentication,.net-3.5,C#,Asp.net,.net,Authentication,.net 3.5,我的代码隐藏中包含以下代码: if (Membership.ValidateUser(username, password)) { bool status = System.Web.HttpContext.Current.User.Identity.IsAuthenticated; 我的用户名和密码验证正确,但状态始终为false。如何实际登录用户以使状态为真?您需要先创建身份验证cookie if(Membership.ValidateUser(username,pas

我的代码隐藏中包含以下代码:

 if (Membership.ValidateUser(username, password))
 {    
     bool status = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;

我的用户名和密码验证正确,但状态始终为false。如何实际登录用户以使状态为真?

您需要先创建身份验证cookie

if(Membership.ValidateUser(username,password))
{
     FormsAuthentication.SetAuthCookie(username,true); 
} //do it in login page
然后,您可以检查用户是否经过身份验证,在您无法知道之前,因为网站是无状态的

来自MSDN中的方法:

ValidateUser提供了一种验证用户名和密码的简单方法 从数据源


请注意,它实际上并没有登录您的用户,它只是告诉您用户名/密码对是否正确。要为您的用户实际创建身份验证cookie,您需要使用。

是否立即设置身份验证cookie?我可以在下一行打电话给isauthenticated,因为它仍然给我false@chris如果在该请求中它不可用,因为信息将在重定向后发送,您可以使用FormsAuthentication.RedirectFromLoginPage或其他重定向