Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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# User.Identity.IsAuthenticated在第一次单击时始终为false?_C#_Asp.net_Asp.net Identity - Fatal编程技术网

C# User.Identity.IsAuthenticated在第一次单击时始终为false?

C# User.Identity.IsAuthenticated在第一次单击时始终为false?,c#,asp.net,asp.net-identity,C#,Asp.net,Asp.net Identity,我有一个asp.net webforms应用程序,它有一个登录屏幕。单击登录按钮时,第一次单击时,User.Identity.IsAuthenticated始终为false。以下是用于登录的代码: protected void SignIn(object sender, EventArgs e) { var userStore = new UserStore<ApplicationUser>(); var userManager = new U

我有一个asp.net webforms应用程序,它有一个登录屏幕。单击登录按钮时,第一次单击时,
User.Identity.IsAuthenticated
始终为false。以下是用于登录的代码:

protected void SignIn(object sender, EventArgs e)
    {
        var userStore = new UserStore<ApplicationUser>();
        var userManager = new UserManager<ApplicationUser>(userStore);
        var user = userManager.Find(UserName.Text, Password.Text);

        if (user != null && user.PasswordRetryCount < 3)
        {
            Session["UserName"] = user.UserName;

            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            authenticationManager.SignIn(new AuthenticationProperties {IsPersistent = false}, userIdentity);

            if (User.Identity.IsAuthenticated) //false on first click
                Response.Redirect("Default.aspx");
        }
    }
受保护的无效登录(对象发送方,事件参数e)
{
var userStore=new userStore();
var userManager=newusermanager(userStore);
var user=userManager.Find(UserName.Text、Password.Text);
if(user!=null&&user.PasswordRetryCount<3)
{
会话[“用户名”]=user.UserName;
var authenticationManager=HttpContext.Current.GetOwinContext().Authentication;
var userIdentity=userManager.CreateIdentity(用户,DefaultAuthenticationTypes.ApplicationOkie);
SignIn(新的AuthenticationProperties{IsPersistent=false},userIdentity);
if(User.Identity.IsAuthenticated)//第一次单击时为false
重定向(“Default.aspx”);
}
}

属性User.Identity.IsAuthenticated将根据其中一个应用程序身份验证事件中来自HttpRequest的cookies进行填充。因此,在您的情况下,此属性将具有“false”值。

在其中一个应用程序身份验证事件中,将根据来自HttpRequest的Cookie填充属性User.Identity.IsAuthenticated。因此,在您的情况下,此属性将具有“false”值。

您已登录用户,但设置IsAuthenticated标志的事件和值在下一个请求之前不会运行。您仍然处于未经身份验证的请求的上下文中


authenticationManager.SignIn(..)的返回值是多少?如果它返回一个bool或任何其他信息,告诉您身份验证是否成功,您可以将其用作
If
语句的标志。

您已登录用户,但设置IsAuthenticated标志的事件和值在下一次请求之前不会运行。您仍然处于未经身份验证的请求的上下文中


authenticationManager.SignIn(..)的返回值是多少?如果它返回一个bool或任何其他信息,告诉您身份验证是否成功,您可以使用它作为
If
语句的标志。

而不是执行以下操作:

if(User.Identity.IsAuthenticated)
要检查用户是否经过身份验证,我检查了
IdentityResult
的以下属性,该属性似乎工作正常:

if(userIdentity.IsAuthenticated)
而不是执行以下操作:

if(User.Identity.IsAuthenticated)
要检查用户是否经过身份验证,我检查了
IdentityResult
的以下属性,该属性似乎工作正常:


if(userIdentity.IsAuthenticated)

我该如何解决这个问题,因为我输入用户名和密码,然后密码字段清除,我必须再次输入,然后我转到网站的另一个部分。看看这个类似的问题:我看到了,但我看不出它如何能帮助我避免让用户输入两次密码。
authenticationManager.SignIn(…)
返回类型无效。我如何避免这种情况,因为我输入的是用户名和密码,然后密码字段清除,我必须再次输入密码,然后我转到网站的另一个部分。看看这个类似的问题:我看到了,但我看不出它如何帮助我避免用户输入两次密码。
authenticationManager.SignIn(…)
返回类型无效。