Asp.net-Mvc-4

DotNetOpenAuth 4.3 和 Google - OpenID 2.0 + OAuth 1.0 已棄用

  • May 4, 2015

如果您想切入正題,那麼問題是:在 asp.net mvc 5 中將 DotNetOpenAuth 與 Google 一起使用的最佳/官方方式是什麼?

大約一年前,我幾乎使用了 OAuth(DotNetOpenAuth oAuth 和 OpenID),因為它為 asp.net MVC 4 開箱即用(就像在範例項目中一樣)。從那以後,我成功地將它用於 google、facebook、yahoo 和 microsoft。但是,最近我在使用者登錄Google時遇到了間歇性問題。我嘗試升級到 MVC 5 和 DotNetOpenAuth 4.3,但我得到了相同的結果。

當我查看Google文件時,我發現了這一點:

重要提示:Google 已棄用對 OAuth 1.0 的支持。如果您使用的是 OpenID 2.0 + OAuth 1.0,我們建議您切換到 Google+ 登錄。Google+ 登錄為 OAuth 2.0 身份驗證機制提供了豐富的社交功能以及對其他 Google 桌面和移動功能的訪問權限。它支持所有Google使用者和透明遷移。詳見遷移Google認證。

我很可能會誤會,因為我認為開箱即用的 asp.net mvc 4 DotNetOpenAuth 使用 OpenID 2.0(我使用 minimumRequiredOpenIdVersion=“V20”)+ OAuth 1.0。我可以在 DotNetOpenAuth 原始碼中看到“產品”下有一個 OAuth 2.0 庫,但我不確定如何使用它。另外,我對 Auth 2.0 有點緊張,因為我讀到的內容不是很互補,而且似乎更容易在腳上開槍(可能沒有根據,但它似乎是一個反復出現的主題)。

對於 Google+,我發現這些說明看起來很簡單,但那是差不多一年前的事了,所以我想知道這是否仍然是最好的方法。我還發現這個 git 儲存庫實現了 Google oauth2。不過,我想知道這是否仍然相關,因為這一切都是從一段時間前開始的。

所以,問題是 - 在 asp.net mvc5 中將 DotNetOpenAuth 與 Google 一起使用的最佳/官方方式是什麼?希望我沒有錯過任何明顯的東西,在這種情況下,只需指向某些連結的指針就可以了。

更新 我發現這個問題和這個問題是相關的。我想我會使用 git 中的 google auth2,除非另有說明。

解析度

我做了以下事情: -

  • 按照接受的答案提供的連結中的步驟進行操作。就是這個連結

登錄後繼續使用 SSL 而不是退回到 HTTP 很重要,您的登錄 cookie 與您的使用者名和密碼一樣保密……登錄後重定向回 HTTP 不會使目前請求或未來請求更快.

  • 在 Nuget 上獲得了最新的 DotNetOpenAuth.GoogleOAuth2。
  • 我查看了這個 msdn 部落格(由同一作者撰寫)關於如何最好地保護網站的建議。基本上,建議添加以下內容,這將強制所有頁面使用 HTTPS:

filters.Add( new System.Web.Mvc.RequireHttpsAttribute() );

最終,這意味著整個站點都是 HTTPS。自進行這些更改以來,該網站一直執行良好。

以下是使用 Google 身份驗證以及其他一些社交集成的推薦方法:

http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

為了使用 oauth2(假設您使用 MVC)

  1. 啟用 Google OpenID 提供程序 打開 App_Start\Startup.Auth.cs 文件並刪除 //app.UseGoogleAuthentication(); 中的註釋字元 啟用 Google 身份驗證。
  2. 在使用其他服務登錄下,點擊 Google。然後,使用者將被重定向到您將在其中輸入憑據的 google 站點。

如果您沒有此文件或文件夾“app_start”,那麼您可能在首次創建解決方案時創建了一個“空白”項目,而不是“網際網路”項目。剛開始時選擇“網際網路應用程序”要容易得多(如果計劃使用外部登錄)。不確定您使用的是什麼編輯器,但 Visual Studio 2012/2013 讓這變得非常簡單!

如果您打算使用現在推薦的 OpenID,這是一個很好的起點: https ://developers.google.com/accounts/docs/OpenID#settingup

最後,如果您可以通過 (Visual Studio) 之類的編輯器訪問 NUGET,您會發現這些任務(例如添加 oAuth-1/2 或 openId)變得非常容易。

如果上述內容不適合您的建構,這是最後一個連結,可以讓您朝著正確的方向前進……如果提供更多詳細資訊,我將非常樂意幫助您找到最佳解決方案。我可以說的一件事是 oauth2 仍然非常相關並在今天的許多應用程序中使用,並且在今天開始一個新項目時實施它不會是錯誤的 - 這將是正確的方法(或至少其中一個正確的方法)……希望其中一些有所幫助,而不僅僅是沿著你已經走過的道路。

希望一切順利。

這就是您將 DotnetOpenAuth 與 Google/OAuth2 一起使用的方式。

首先,參考 Nuget 的 DotnetOpenAuth.Ultimate 包。

然後創建提供者類和配置文件模型類

public class GoogleClient : WebServerClient
{
   private static readonly AuthorizationServerDescription GoogleDescription = 
       new AuthorizationServerDescription
   {
       TokenEndpoint = new Uri( "https://accounts.google.com/o/oauth2/token" ),
       AuthorizationEndpoint = new Uri( "https://accounts.google.com/o/oauth2/auth" ),
       ProtocolVersion = ProtocolVersion.V20
   };

   public const string ProfileEndpoint = "https://www.googleapis.com/oauth2/v1/userinfo";

   public const string ProfileScope = "https://www.googleapis.com/auth/userinfo.profile";
   public const string EmailScope = "https://www.googleapis.com/auth/userinfo.email";

   public GoogleClient()
       : base( GoogleDescription )
   {
   }
}

public class GoogleProfileAPI
{
   public string email { get; set; }

   private static DataContractJsonSerializer jsonSerializer = 
       new DataContractJsonSerializer( typeof( GoogleProfileAPI ) );

   public static GoogleProfileAPI Deserialize( Stream jsonStream )
   {
       try
       {
           if ( jsonStream == null )
           {
               throw new ArgumentNullException( "jsonStream" );
           }

           return (GoogleProfileAPI)jsonSerializer.ReadObject( jsonStream );
       }
       catch ( Exception ex )
       {
           return new GoogleProfileAPI();
       }
   }
}

然後,在您的登錄頁面(登錄控制器)中有以下程式碼:

   private static readonly GoogleClient googleClient = new GoogleClient
   {
       ClientIdentifier = "client_id",
       ClientCredentialApplicator = ClientCredentialApplicator.PostParameter( "client_secret" )
   };

       // Page_Load of login page if WebForms
       // Login action of the Account controller if MVC 

       IAuthorizationState authorization = googleClient.ProcessUserAuthorization();
       if ( authorization == null )
       {
           // Kick off authorization request
           // Google will redirect back here
           Uri uri = new Uri( "http://your.application.address/login" );
           googleClient.RequestUserAuthorization( returnTo: uri, 
               scope: new[] { GoogleClient.ProfileScope, GoogleClient.EmailScope } );
       }
       else
       {
           // authorization. we have the token and 
           // we just go to profile APIs to get email (and possibly other data)
           var request =
               WebRequest.Create(
                   string.Format( "{0}?access_token={1}", 
                   GoogleClient.ProfileEndpoint, 
                   Uri.EscapeDataString( authorization.AccessToken ) ) );
           using ( var response = request.GetResponse() )
           {
               using ( var responseStream = response.GetResponseStream() )
               {
                   var profile = GoogleProfileAPI.Deserialize( responseStream );
                   if ( profile != null &&
                       !string.IsNullOrEmpty( profile.email ) )
                       FormsAuthentication.RedirectFromLoginPage( profile.email, false );
               }
           }
       }

引用自:https://stackoverflow.com/questions/21420019