Asp.net-Mvc
如何使用 ASP.NET MVC 5 和 OWIN 獲取 Facebook 名字和姓氏值?
我知道提供了一個“名稱”欄位,但我更願意顯式訪問名字和姓氏。有人可以幫忙嗎?我仍然在 ASP.Net MVC 周圍。
在您的 Startup.Auth.cs
ConfigureAuth(IAppBuilder app)方法中,為 Facebook 設置以下內容:var x = new FacebookAuthenticationOptions(); x.Scope.Add("email"); x.AppId = "*"; x.AppSecret = "**"; x.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")); } } }; x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie; app.UseFacebookAuthentication(x); /* app.UseFacebookAuthentication( appId: "*", appSecret: "*"); * */然後使用它來訪問使用者的登錄資訊:
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();然後如下獲取名字:
var firstNameClaim = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:first_name");