Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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
ASP.NET标识以及如何设置Request.IsAuthenticated_Asp.net_Authentication_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

ASP.NET标识以及如何设置Request.IsAuthenticated

ASP.NET标识以及如何设置Request.IsAuthenticated,asp.net,authentication,asp.net-mvc-5,asp.net-identity,Asp.net,Authentication,Asp.net Mvc 5,Asp.net Identity,我在VS2013中使用默认的MVC5项目。在_LoginPartial.cshtml中,它对Request.isAuthenticated进行检查,并对返回的内容进行分支。我的问题是,这是从哪里开始的?我修改了我的代码,以便能够使用谷歌直接从主页登录,但在它这样做并返回索引后,这个IsAuthenticated值仍然是假的 我创建了一个“使用Google登录”链接,单击后,我将其指向现有的ExternalLogin()操作。在这之后,它将调用ExternalLoginCallback(),我稍微

我在VS2013中使用默认的MVC5项目。在_LoginPartial.cshtml中,它对Request.isAuthenticated进行检查,并对返回的内容进行分支。我的问题是,这是从哪里开始的?我修改了我的代码,以便能够使用谷歌直接从主页登录,但在它这样做并返回索引后,这个IsAuthenticated值仍然是假的

我创建了一个“使用Google登录”链接,单击后,我将其指向现有的ExternalLogin()操作。在这之后,它将调用ExternalLoginCallback(),我稍微修改了它,从GUID自动创建一个用户名,然后登录。然而,IsAuthenticated仍然是假的。我错过了什么

代码确实到达了ExternalLoginCallback()内部的SignInAsync(),并且没有发生错误,它返回到索引时很好,因此不确定出了什么问题

    // POST: /Account/ExternalLogin
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(string provider, string returnUrl)
    {
        // Request a redirect to the external login provider
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
    }

    //
    // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        // Sign in the user with this external login provider if the user already has a login
        var user = await UserManager.FindAsync(loginInfo.Login);
        if (user != null)
        {
            await SignInAsync(user, isPersistent: false);
            return RedirectToLocal(returnUrl);
        }
        else
        {
            // Get the information about the user from the external login provider
            var info = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return View("ExternalLoginFailure");
            }
            //var user = new ApplicationUser() { UserName = model.UserName };
            user = new ApplicationUser() { UserName = Guid.NewGuid().ToString().Replace("-", "") };
            var result = await UserManager.CreateAsync(user);
            if (result.Succeeded)
            {
                result = await UserManager.AddLoginAsync(user.Id, info.Login);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToLocal(returnUrl);
                }
            }
            AddErrors(result);
        }

        return View();
    }
//POST:/Account/ExternalLogin
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共操作结果外部登录(字符串提供程序、字符串返回URL)
{
//请求重定向到外部登录提供程序
returnnewchallengeresult(提供者,Url.Action(“ExternalLoginCallback”,“Account”,new{ReturnUrl=ReturnUrl}));
}
//
//获取:/Account/ExternalLoginCallback
[异名]
公共异步任务ExternalLoginCallback(字符串返回URL)
{
var loginInfo=await AuthenticationManager.GetExternalLoginInfoAsync();
if(loginInfo==null)
{
返回重定向操作(“登录”);
}
//如果用户已经登录,请使用此外部登录提供程序登录该用户
var user=await UserManager.FindAsync(loginInfo.Login);
如果(用户!=null)
{
等待信号同步(用户,ispersist:false);
返回重定向到本地(returnUrl);
}
其他的
{
//从外部登录提供程序获取有关用户的信息
var info=await AuthenticationManager.GetExternalLoginInfoAsync();
if(info==null)
{
返回视图(“外部登录失败”);
}
//var user=new ApplicationUser(){UserName=model.UserName};
user=newapplicationuser(){UserName=Guid.NewGuid().ToString().Replace(“-”,”)};
var result=await UserManager.CreateAsync(用户);
if(result.successed)
{
结果=wait UserManager.AddLoginAsync(user.Id,info.Login);
if(result.successed)
{
等待信号同步(用户,ispersist:false);
返回重定向到本地(returnUrl);
}
}
加法器(结果);
}
返回视图();
}

SignInAsync导致创建cookie,然后框架读取cookie,以便在加载页面时将其设置为IsAuthenticated


很可能,您的cookie已损坏,这导致cookie设置不正确。清除cookies可能会解决此问题。

您是否尝试过清除cookies?已清除cookies。我知道它是有效的,因为当它进入谷歌获取我的授权时,我必须输入我的电子邮件和pw,因为它被清除了。当它返回时,我没有收到任何错误,并且Request.IsAuthenticated在索引页上仍然为false。