繁体   English   中英

无法使用Owin在ASP.NET中加载facebook身份

[英]Can't load facebook identity in ASP.NET using Owin

我有加载Facebook身份的问题。 该页面正确地重定向到Facebook,但当我回到我的网站时,我无法读取Owin的身份。

这是我的Startup.Auth.cs

var cookieOptions = new CookieAuthenticationOptions
{
    LoginPath = new PathString("/default.aspx")
};

app.UseCookieAuthentication(cookieOptions);
app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType);
var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
    AppId = ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First() != null ? ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First().AppId : string.Empty,
    AppSecret = ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First() != null ? ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First().AppSecretKey : string.Empty,
    SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType(),
    CallbackPath = new PathString("/Default.aspx/")
};

facebookAuthenticationOptions.Provider = new FacebookAuthenticationProvider
{
    OnAuthenticated = async context =>
    {
        context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
        foreach (var claim in context.User)
        {
            var claimType = string.Format("urn:facebook:{0}", claim.Key);
            string claimValue = claim.Value.ToString();
            if (!context.Identity.HasClaim(claimType, claimValue))
                context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
        }
    }
};

facebookAuthenticationOptions.Scope.Add("email");
facebookAuthenticationOptions.Scope.Add("public_profile");

app2.UseFacebookAuthentication(facebookAuthenticationOptions);

我的PageBase.cs来处理登录:

var ctx = Context.GetOwinContext();
var user = ctx != null ? ctx.Authentication.User : new ClaimsPrincipal();
if (user != null && user.Identities.Count() > 0 && user.Identity.IsAuthenticated)
{
    var claimsPrincipal = new ClaimsPrincipal(user.Identity);
    Thread.CurrentPrincipal = claimsPrincipal;
    var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;

    if (identity.Claims.Count() > 0)
    {
        switch (identity.Claims.First().OriginalIssuer)
        {
            case "Facebook":

                if (!featureExternalLoginFacebook) { break; }

                id = identity.Claims.Where(c => c.Type == "urn:facebook:id").Select(c => c.Value).SingleOrDefault();
                name = identity.Claims.Where(c => c.Type == ClaimTypes.Name).Select(c => c.Value).SingleOrDefault();
                accessToken = identity.Claims.Where(c => c.Type == "FacebookAccessToken").Select(c => c.Value).SingleOrDefault();

                var url = string.Format("https://graph.facebook.com/v2.5/{0}?fields=email,first_name,last_name&access_token={1}", id, accessToken);

                var req = WebRequest.Create(url);
                req.Method = "GET";
                req.ContentType = "application/json";

                using (var res = new StreamReader(req.GetResponse().GetResponseStream()))
                {
                    dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(res.ReadToEnd());

                    if (result.id != null && result.id == id)
                    {
                        email = result.email != null ? result.email : string.Empty;
                        firstName = result.first_name != null ? result.first_name : string.Empty;
                        lastName = result.last_name != null ? result.last_name : string.Empty;
                    }
                }

                break;
        }

        externalApp = identity.Claims.First().OriginalIssuer;

        if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(email))
        {
            return true;
        }
    }

在user.Identities中,我将IsAuthenticated视为真实。 现在我总是假的,我没有身份。对Facebook的声明。

如何获取有关已登录用户的Facebook用户信息,请参阅。 我如何获得Facebook身份?

Facebook最近弃用了他们的一些OAuth端点。 如果您使用的是NuGet软件包Microsoft.Owin.Security.Facebook 3.0.1,则需要更新到最新版本(它还将更新多个相关软件包)。

  • 右键单击项目
  • 选择“管理NuGet包”
  • 选择“已安装”
  • 搜索“Facebook”或从列表中找到包
  • 选择“Microsoft.Owin.Security.Facebook”并单击“更新”

参考: https//github.com/aspnet/AspNetKatana/issues/38

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM