【问题标题】:Request additional claims Owin Security请求额外索赔 Owin Security
【发布时间】:2014-04-28 17:43:45
【问题描述】:

我正在尝试使用 OWIN Security 从 google OAuth 握手中检索其他信息。

我有以下要求向 Google 请求用户个人资料声明,并且 google 权限页面反映了此声明已请求。

var googleConfig = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions
{
    ClientId = ClientId",
    ClientSecret = "Secret"
};

googleConfig.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");

app.UseGoogleAuthentication(googleConfig);

但是,当我使用 AuthenticationManager.GetExternalLoginInfoAsync(); 收到回复时,用户只有一个名称声明。

我需要做什么才能在登录时从谷歌取回用户配置文件数据?

【问题讨论】:

    标签: c# oauth-2.0 google-oauth owin


    【解决方案1】:

    您需要访问 Provider 的 OnAuthenticate 事件中的其他声明。在那里,上下文参数包含您在范围中要求的这些附加属性。例如,使用 Facebook 时:

    var fb = new FacebookAuthenticationOptions
    {
        AppId = "...",
        AppSecret = "...",
        AuthenticationType = "Facebook",
        SignInAsAuthenticationType = "ExternalCookie",
        Provider = new FacebookAuthenticationProvider
        {
            OnAuthenticated = async ctx =>
            {
                if (ctx.User["birthday"] != null)
                {
                    ctx.Identity.AddClaim(new Claim(ClaimTypes.DateOfBirth, ctx.User["birthday"].ToString()));
                }
            }
        }
    };
    fb.Scope.Add("user_birthday");
    app.UseFacebookAuthentication(fb);
    

    【讨论】:

    • 嘿,这看起来完全正确,但声明仍然没有传播到用户令牌上。我可以在 OnAuthenticated 方法中看到它们,但在请求中它们不存在。看起来身份验证上下文中的身份与用户对象上的身份不同
    • 那么问题可能出在从外部 cookie 转换到应用程序 cookie 的 MVC 示例代码中。我不知道你的项目中的代码是什么样子的。
    • 我找到了你的入门博文(brockallen.com/2014/01/09/… 谢谢它的好)我需要的是Request.GetOwinContext().Authentication.AuthenticateAsync("ExternalCookie").Result
    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2016-02-19
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多