阅读Facebook声明 - 这是一个好方法吗?

时间:2014-02-07 02:59:31

标签: c# asp.net-mvc facebook claims-based-identity owin

我正在尝试在验证用户身份后阅读FB声明。到目前为止,这是我的代码:

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

    dynamic emailClaim = JsonConvert.DeserializeObject(externalIdentity.Result.Claims.Single(c => c.Type == ClaimTypes.Email).Value);
    dynamic locationClaim = JsonConvert.DeserializeObject(externalIdentity.Result.Claims.Single(c => c.Type == "urn:facebook:location").Value);
    dynamic genderClaim = JsonConvert.DeserializeObject(externalIdentity.Result.Claims.Single(c => c.Type == "urn:facebook:gender").Value);
    dynamic firstNameClaim = JsonConvert.DeserializeObject(externalIdentity.Result.Claims.Single(c => c.Type == "urn:facebook:first_name").Value);
    dynamic birthdateClaim = JsonConvert.DeserializeObject(externalIdentity.Result.Claims.Single(c => c.Type == "urn:facebook:birthday").Value);

    var email = emailClaim.email;
    var city = locationClaim.name.ToString().Split(',')[0];
    var country = locationClaim.name.ToString().Split(',')[1];

    var gender = genderClaim.gender;
    var firstname = firstNameClaim.first_name;
    var birthdate = DateTime.ParseExact(birthdateClaim.birthday, "MM/dd/yyyy", CultureInfo.InvariantCulture);

正如您所看到的,我正在将FB声明转换为动态对象,以便稍后可以更容易地引用它。

你认为这是一个好方法吗?我打算读取提供程序并应用策略模式来封装这个逻辑。听起来不错?或者我应该重构其他什么?

0 个答案:

没有答案