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或其他重定向