Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# 避免手动设置FormsAuthenticationCookie_C#_Asp.net_Webforms_Forms Authentication_Membership Provider - Fatal编程技术网

C# 避免手动设置FormsAuthenticationCookie

C# 避免手动设置FormsAuthenticationCookie,c#,asp.net,webforms,forms-authentication,membership-provider,C#,Asp.net,Webforms,Forms Authentication,Membership Provider,我曾在不同的时间使用以下工具进行过不良行为: FormsAuthentication.SetAuthCookie(user.UserName, true); Net将如何设置cookie 我试过这个:() 但是我的User.Identity.IsAuthenticated始终为false 给出了什么?首先确保在web配置文件中启用了表单身份验证 <authentication mode="Forms" /> SetAuthCookie无法工作的原因是您没有在web配置中启用for

我曾在不同的时间使用以下工具进行过不良行为:

FormsAuthentication.SetAuthCookie(user.UserName, true);
Net将如何设置cookie

我试过这个:()

但是我的User.Identity.IsAuthenticated始终为false


给出了什么?

首先确保在web配置文件中启用了表单身份验证

<authentication mode="Forms" />

SetAuthCookie无法工作的原因是您没有在web配置中启用forms authetocation。在system.web标记的web配置中包括以下设置:
我一直都有它,而且它主要起作用。请记住,您也可以通过调用
FormsAuthentication.RedirectFromLoginPage
方法来设置cookie,这将设置身份验证票证/cookie,并将使用重定向到请求的URL或默认URL。是的,我做到了。这样就不会手动设置cookie并自行设置了吗?是的,该方法封装了cookie创建功能。
if (Membership.ValidateUser(userName, password))//or whatever method you use
 {
    // Log the user into the site
    FormsAuthentication.RedirectFromLoginPage(userName, false);
 }