簡體   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