Asp.net mvc MVC 5 OWIN-外部登录时IsAuthenticated为false(QQ连接)

Asp.net mvc MVC 5 OWIN-外部登录时IsAuthenticated为false(QQ连接),asp.net-mvc,oauth-2.0,asp.net-mvc-5,owin,Asp.net Mvc,Oauth 2.0,Asp.net Mvc 5,Owin,我希望有人能帮我解决这个问题——我快疯了 我正在尝试使用tinysnake的QQ连接提供商通过QQ连接(OAuth 2.0)进行外部登录: 一切似乎都进行得很顺利-我可以通过我的QQ帐户登录,并通过适当的声明等回复到我的ExternalLoginCallBack方法。 我使用这些值通过IAAuthenticationManager登录用户-一切顺利。然而,当我将用户重定向到另一个页面并检查他是否登录时,我从IsAuthenticated值中得到一个假值。。。我也看不到我之前提出的任何主张 这可能

我希望有人能帮我解决这个问题——我快疯了

我正在尝试使用tinysnake的QQ连接提供商通过QQ连接(OAuth 2.0)进行外部登录:

一切似乎都进行得很顺利-我可以通过我的QQ帐户登录,并通过适当的声明等回复到我的ExternalLoginCallBack方法。 我使用这些值通过IAAuthenticationManager登录用户-一切顺利。然而,当我将用户重定向到另一个页面并检查他是否登录时,我从IsAuthenticated值中得到一个假值。。。我也看不到我之前提出的任何主张

这可能是一个简单的解决方案,但我现在看不到:)

一些代码:

AuthConfig:

public static void ConfigureAuthentication(IAppBuilder app)
{
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    // Normal cookie sign in
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        AuthenticationMode = AuthenticationMode.Active
    });

    // QQ CONNECT
    app.UseQQConnectAuthentication(
        appId: "XXXXXX",
        appSecret: "XXXXXXXXXXXXXXXXX");
}
//
// POST: /Account/ExternalLogin
[System.Web.Mvc.HttpPost]
[System.Web.Mvc.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
[System.Web.Mvc.AllowAnonymous]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var ctx = Request.GetOwinContext();
    var result = ctx.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie).Result;
    var claims = result.Identity.Claims.ToList();
    var name = claims.First(i => i.Type == "urn:qqconnect:name");

    claims.Add(new Claim(ClaimTypes.AuthenticationMethod, "QQ"));
    claims.Add(new Claim(ClaimTypes.Name, name.Value));

    var ci = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ExternalCookie);
    ctx.Authentication.SignIn(ci);

    // DO OTHER STUFF HERE

    return Redirect("~/");
}
public ActionResult Index()
{
    var model = new HomeViewModel();

    var ctx = Request.GetOwinContext();
    if (ctx.Authentication.User.Identity.IsAuthenticated)  // <-- THIS RETURNS FALSE
    {
        var claimsIdentity = User.Identity as ClaimsIdentity;
        model.Name = claimsIdentity.FindFirst(ClaimTypes.Name).Value;
        model.IsAuthenticated = true;
    }

    return View(model);
}
会计控制器:

public static void ConfigureAuthentication(IAppBuilder app)
{
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    // Normal cookie sign in
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        AuthenticationMode = AuthenticationMode.Active
    });

    // QQ CONNECT
    app.UseQQConnectAuthentication(
        appId: "XXXXXX",
        appSecret: "XXXXXXXXXXXXXXXXX");
}
//
// POST: /Account/ExternalLogin
[System.Web.Mvc.HttpPost]
[System.Web.Mvc.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
[System.Web.Mvc.AllowAnonymous]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var ctx = Request.GetOwinContext();
    var result = ctx.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie).Result;
    var claims = result.Identity.Claims.ToList();
    var name = claims.First(i => i.Type == "urn:qqconnect:name");

    claims.Add(new Claim(ClaimTypes.AuthenticationMethod, "QQ"));
    claims.Add(new Claim(ClaimTypes.Name, name.Value));

    var ci = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ExternalCookie);
    ctx.Authentication.SignIn(ci);

    // DO OTHER STUFF HERE

    return Redirect("~/");
}
public ActionResult Index()
{
    var model = new HomeViewModel();

    var ctx = Request.GetOwinContext();
    if (ctx.Authentication.User.Identity.IsAuthenticated)  // <-- THIS RETURNS FALSE
    {
        var claimsIdentity = User.Identity as ClaimsIdentity;
        model.Name = claimsIdentity.FindFirst(ClaimTypes.Name).Value;
        model.IsAuthenticated = true;
    }

    return View(model);
}
//
//POST:/Account/ExternalLogin
[System.Web.Mvc.HttpPost]
[System.Web.Mvc.AllowAnonymous]
[ValidateAntiForgeryToken]
公共操作结果外部登录(字符串提供程序、字符串返回URL)
{
//请求重定向到外部登录提供程序
returnnewchallengeresult(提供者,Url.Action(“ExternalLoginCallback”,“Account”,new{ReturnUrl=ReturnUrl}));
}
//
//获取:/Account/ExternalLoginCallback
[System.Web.Mvc.AllowAnonymous]
[主机身份验证(DefaultAuthenticationTypes.ExternalCookie)]
公共异步任务ExternalLoginCallback(字符串返回URL)
{
var ctx=Request.GetOwinContext();
var result=ctx.Authentication.authenticateSync(DefaultAuthenticationTypes.ExternalCookie).result;
var claims=result.Identity.claims.ToList();
var name=claims.First(i=>i.Type==“urn:qqconnect:name”);
添加(新索赔(ClaimTypes.AuthenticationMethod,“QQ”);
添加(新索赔(ClaimTypes.Name,Name.Value));
var ci=新的ClaimsEntity(索赔、DefaultAuthenticationTypes.ExternalCookie);
ctx.Authentication.SignIn(ci);
//在这里做其他事情
返回重定向(“~/”);
}
到目前为止一切似乎都进展顺利

家庭控制器:

public static void ConfigureAuthentication(IAppBuilder app)
{
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    // Normal cookie sign in
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        AuthenticationMode = AuthenticationMode.Active
    });

    // QQ CONNECT
    app.UseQQConnectAuthentication(
        appId: "XXXXXX",
        appSecret: "XXXXXXXXXXXXXXXXX");
}
//
// POST: /Account/ExternalLogin
[System.Web.Mvc.HttpPost]
[System.Web.Mvc.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
[System.Web.Mvc.AllowAnonymous]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var ctx = Request.GetOwinContext();
    var result = ctx.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie).Result;
    var claims = result.Identity.Claims.ToList();
    var name = claims.First(i => i.Type == "urn:qqconnect:name");

    claims.Add(new Claim(ClaimTypes.AuthenticationMethod, "QQ"));
    claims.Add(new Claim(ClaimTypes.Name, name.Value));

    var ci = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ExternalCookie);
    ctx.Authentication.SignIn(ci);

    // DO OTHER STUFF HERE

    return Redirect("~/");
}
public ActionResult Index()
{
    var model = new HomeViewModel();

    var ctx = Request.GetOwinContext();
    if (ctx.Authentication.User.Identity.IsAuthenticated)  // <-- THIS RETURNS FALSE
    {
        var claimsIdentity = User.Identity as ClaimsIdentity;
        model.Name = claimsIdentity.FindFirst(ClaimTypes.Name).Value;
        model.IsAuthenticated = true;
    }

    return View(model);
}
public ActionResult Index()
{
var模型=新的HomeViewModel();
var ctx=Request.GetOwinContext();

如果(ctx.Authentication.User.Identity.IsAuthenticated)/根据我的理解,您所经历的是意料之中的。过于简单化了:

  • 应用程序获取外部信息并使用它创建外部cookie
  • 外部cookie发送到您的应用程序时,假设它只是一个临时cookie,将用于查找有关用户的任何其他本地信息,然后转换为本地[应用程序]cookie

  • 请参阅,以获得更全面的细分。

    如果有人可以澄清的话,也有类似的问题。