2015-01-09 16 views
5

Tôi đã thêm phạm vi sau đây để truy cập dữ liệu từ người dùngngày tháng năm sinh Lấy từ Google OAuth API trong MVC

#region Google 
var googleAuthenticationOptions = new GoogleOAuth2AuthenticationOptions() 
     { 
      ClientId = ConfigurationManager.AppSettings["Google_AppID"], 
      ClientSecret = ConfigurationManager.AppSettings["Google_AppSecret"] 
     }; 
     googleAuthenticationOptions.Scope.Add("profile"); 
     googleAuthenticationOptions.Scope.Add("email"); 
googleAuthenticationOptions.Provider = new GoogleOAuth2AuthenticationProvider() 
     { 
      OnAuthenticated = async context => 
      { 
       //context.Identity.AddClaim(new Claim("birthday", context.User.GetValue("birthday").ToString())); 
       string claimType; 
       bool bAddClaim = false; 
       foreach (var claim in context.User) 
       { 

        claimType = string.Empty; 
        bAddClaim = false; 
        switch (claim.Key) 
        { 
         case "given_name": 
          claimType = "FirstName"; 
          bAddClaim = true; 
          break; 
         case "family_name": 
          claimType = "LastName"; 
          bAddClaim = true; 
          break; 
         case "gender": 
          claimType = "gender"; 
          bAddClaim = true; 
          break; 
        } 

        if (bAddClaim) 
        { 
         string claimValue = claim.Value.ToString(); 
         if (!context.Identity.HasClaim(claimType, claimValue)) 
          context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Google")); 
        } 
       } 
      } 
     }; 

Trong khi gỡ lỗi tại context.user ở bên vòng lặp foreach bằng cách đặt breakpoint Tôi nhận được sau thông tin

 
{ 
"sub": "101111724115925537597", 
"name": "Alok Nath", 
"given_name": "Alok", 
"family_name": "Nath", 
"profile": "https://plus.google.com/101111724115925537597", 
"picture":"https://lh3.googleusercontent.com/XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/ 
      photo.jpg", 
"email": "[email protected]", 
"email_verified": true, 
"gender": "male", 
"locale": "en" 
} 

Vì vậy, ngay cả sau khi thêm phạm vi "hồ sơ", tôi không nhận được ngày sinh như một phần của giá trị trả lại. Tôi đã đảm bảo rằng tiểu sử có Chế độ hiển thị sinh nhật được đặt thành công khai, tức là tiểu sử hiển thị với tất cả mọi người. Có bất kỳ phạm vi cụ thể hoặc cài đặt cụ thể nào tôi cần thực hiện để truy xuất Sinh nhật từ Google không?

Hoặc là có cách nào để giải quyết vấn đề này xin vui lòng gửi giải pháp

+0

Bạn đã thử thêm gói nuget ** Thư viện khách hàng của Google.Apis.OAuth2.v2 ** vào dự án của bạn chưa? Điều này cũng cung cấp cho bạn một enum cho phạm vi của bạn vì vậy thay vì sử dụng chuỗi, bạn chỉ có thể nói: 'Scope = {Oauth2Service.Scope.UserinfoProfile, Oauth2Service.Scope.UserinfoEmail}' –

Trả lời

Các vấn đề liên quan