2014-09-22 98 views
5

我試圖在MVC 5網絡應用程序中實現Google身份驗證。身份驗證工作正常,但我會檢索個人資料和圖片信息。使用GoogleOAuth2AuthenticationOptions得到了redirect_uri_mismatch錯誤

要做到這一點,我添加了一個GoogleOAuth2AuthenticationOptions對象以指定其他聲明:

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions 
{ 
    ClientId = "xxxxxxxxxxxxxxxx", 
    ClientSecret = "xxxxxxxxxxxxxxxxx", 
    CallbackPath = new PathString("/Account/LoginCallback"), 
    Provider = new GoogleOAuth2AuthenticationProvider() 
    { 
     OnAuthenticated = async context => 
     { 
      context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString())); 
      context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString())); 
     } 
    } 
}; 

app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions); 

但它會導致一個錯誤的URL生成: '?'

http://admin.localhost.com/Account/LoginCallback&state=Gs-otJmI79bgWA3_qJzDcGziWnkRCOf7JRoCUDCIz0jv4IIvDdoZlZzVSq2kZxfaPFDmv9hbZGp5q1Aq8mpLPguKnCF31twHj8NQCMv_NrgZzvKwaelmZr_HwY_bdj8h1ICFrkGTKLJ1saEYDbFJ2CJxvDkyBL2iygQmTXQTs-aUiL4yWe5_7dZQOjP_mDUSW-GXns3wr7Okwkoj8VEITJTUz9nAbrBd_N_7puTMlHU&client_id=xxxxxxxxxxxxxxxx

沒有在參數之前,這會導致redirect_uri_mismatch。

然而,當我使用簡單:

app.UseGoogleAuthentication(
    clientId : "xxxxxxxxxxxxxxxxxxx", 
    clientSecret : "xxxxxxxxxxxxxxxxx"); 

它的工作。

有什麼想法?

回答

5

只用這麼多。

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions 
      { 
       ClientId = "MYCLIENTID", 
       ClientSecret = "MYSECRET", 
      }; 
    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions); 

這種方法似乎是自動使用在address.To的登入,要求谷歌在谷歌控制檯修復這種變化谷歌回調位置指向這個地址。

添加RouteConfig文件

routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "LoginCallback" }); 
+0

非常感謝,我得到了我想要感謝您的回調URL,併爲資料圖片,我只是用這裏的解決方案mentionned:http://stackoverflow.com/questions/22820349/how-to-get-google-plus-profile-picture-in-c-sharp-mvc-authentication – feuille94 2014-09-23 23:11:06

+0

在MVC5中不需要這條路由,只需要在@Saines中添加條目就可以了... – 2017-05-27 07:23:19

1

使用此下面的代碼片段這是工作的罰款只需更換客戶端ID和ClientSecret會爲你工作。

 var googleOptions = new GoogleOAuth2AuthenticationOptions() 
     { 
      ClientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 
      ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 
      Provider = new GoogleOAuth2AuthenticationProvider() 
      { 
       OnAuthenticated = (context) => 
       { 
        context.Identity.AddClaim(new Claim("urn:google:name", context.Identity.FindFirstValue(ClaimTypes.Name))); 
        context.Identity.AddClaim(new Claim("urn:google:email", context.Identity.FindFirstValue(ClaimTypes.Email))); 
        //This following line is need to retrieve the profile image 
        context.Identity.AddClaim(new System.Security.Claims.Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google")); 

        return Task.FromResult(0); 
       } 
      } 
     }; 

     app.UseGoogleAuthentication(googleOptions); 

如果仍然存在錯誤

然後在console.developers.google.com的URI您已經註冊需要是假設,如果您的應用程序如下圖所示

http://localhost:2625/

URI如下所示更改。

只需添加[登入,谷歌在URI月底

http://localhost:2625/signin-google

最後保存。

Making Change in Authorized redirect URIs at console.developers.google.com

相關問題