[英]How do i get “First Name” and “Last Name” from sharepoint list using c#
[英]how to get first_name and last_name from facebook OAuth in MVC 5.0 C#?
我需要從Facebook v2_5 OAuth獲取first_name和last_name。 在C#中,MVC5。我正在使用Microsoft.Owin.Security.Facebook 3.0.1.0。
`var record = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:name");`
我正在使用此名稱,但是我分別需要first_name和last_name。 任何幫助將不勝感激。
它稱為“聲明性字段”,請參見v2.4的變更日志: https : //developers.facebook.com/docs/graph-api/changelog/archive#v2_4_new_features
這將是獲取first_name
和last_name
的API調用:
/me?fields=name,first_name,last_name
僅使用端點是不夠的,並且對於當前的OWIN Facebook庫3.1.0來說,這是不必要的-您現在可以在傳遞的選項中指定字段。
我必須使用一個自定義的身份驗證提供程序來解決OWIN Facebook庫中的一個問題,Facebook返回該字段,但是OWIN lib無法解析並為您收集它們。 我懷疑這是因為庫的設計使您可以提出索賠,然后檢索對該索賠的響應; 但是如果您僅命名字段,則由於某種原因它根本無法解析出來。
在Startup.Auth中:
var facebook = new FacebookAuthenticationOptions
{
Provider = Brass9.OwinVisitor.Auth.Facebook.
FirstLastNameFacebookAuthenticationProvider.SplitFirstLastName(),
#if DEBUG
AppId = //
AppSecret = //
#else
AppId = //
AppSecret = //
#endif
};
//id,name,email,first_name,last_name
var fbScope = facebook.Scope;
fbScope.Add("email");
//fbScope.Add("first_name"); // Uncommenting this line will break auth
facebook.Fields.Add("id");
facebook.Fields.Add("email");
facebook.Fields.Add("name");
facebook.Fields.Add("first_name");
facebook.Fields.Add("last_name");
app.UseFacebookAuthentication(facebook);
因此,到目前為止,我已經使用了新的OWIN Fields選項來指定id,電子郵件,名稱,first_name,last_name-而不是傳入自定義的端點。我還指定了一個自定義的AuthenticationProvider。 這里是:
namespace Brass9.OwinVisitor.Auth.Facebook
{
public static class FirstLastNameFacebookAuthenticationProvider
{
public static FacebookAuthenticationProvider SplitFirstLastName()
{
return 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"));
}
}
};
}
}
}
這彌補了OWIN lib無法解析這些值,並將它們作為聲明存儲在loginInfo用戶對象中的不足。
最后,為了方便起見,我使用了一個last類來跨提供程序以規范的方式獲取這些值,因為不同的提供程序使用不同的模式提供了名字和姓氏:
namespace Brass9.OwinVisitor.Auth
{
public class ReducedClaims
{
protected Dictionary<string, string> claims;
public ReducedClaims(IEnumerable<System.Security.Claims.Claim> claims)
{
var _claims = claims.ToArray();
this.claims = new Dictionary<string, string>(_claims.Length);
foreach(var claim in _claims)
{
this.claims.Add(claim.Type, claim.Value);
}
}
public ReducedClaims(ExternalLoginInfo loginInfo)
: this(loginInfo.ExternalIdentity.Claims)
{}
//http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname
//http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname
protected string chainValue(params string[] keys)
{
string val = null;
foreach(var key in keys)
{
if (claims.TryGetValue(key, out val))
return val;
}
return val;
}
// TODO: Instead detect which service it is then use the proper string instead of just milling through guesses?
public string FirstName { get { return chainValue(
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname", // Google
"urn:facebook:first_name" // Facebook
); } }
public string LastName { get { return chainValue(
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", // Google
"urn:facebook:last_name" // Facebook
); } }
}
}
您可以這樣稱呼:
var reducedClaims = new ReducedClaims(loginInfo.ExternalIdentity.Claims);
var firstName = reducedClaims.FirstName;
var lastName = reducedClaims.LastName;
克里斯的答案略有不同。 訣竅是使用Facebook發送的first_name
和last_name
字段中的值添加具有眾所周知類型的聲明。 由於某些原因,Microsoft 庫無法執行此操作。
var facebookOptions = new FacebookAuthenticationOptions
{
AppId = "*",
AppSecret = "*",
};
const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
facebookOptions.Provider = new FacebookAuthenticationProvider
{
OnAuthenticated = context =>
{
void addClaim(
FacebookAuthenticatedContext ctx,
string faceBookClaim,
string aspNetClaim)
{
if (context.User.TryGetValue(faceBookClaim, out JToken t))
{
ctx.Identity.AddClaim(
new Claim(
aspNetClaim,
t.ToString(),
XmlSchemaString,
facebookOptions.AuthenticationType));
}
}
addClaim(context, "first_name", ClaimTypes.GivenName);
addClaim(context, "last_name", ClaimTypes.Surname);
return Task.CompletedTask;
}
};
facebookOptions.Scope.Add("public_profile");
facebookOptions.Scope.Add("email");
facebookOptions.Fields.Add("email");
facebookOptions.Fields.Add("name");
facebookOptions.Fields.Add("first_name");
facebookOptions.Fields.Add("last_name");
app.UseFacebookAuthentication(facebookOptions);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.