问题 DotNetOpenAuth 4.3和Google - OpenID 2.0 + OAuth 1.0已弃用


如果你想切入追逐,问题是:在asp.net mvc 5中使用DotNetOpenAuth与谷歌的最佳/官方方式是什么?

大约一年前,我使用了OAuth(DotNetOpenAuth oAuth和OpenID),就像asp.net MVC 4开箱即用(就像在示例项目中一样)。从那时起,我成功地将它用于谷歌,脸谱,雅虎和微软。但是,最近我一直在 间歇性的问题 用户登录谷歌。我已经尝试升级到MVC 5和DotNetOpenAuth 4.3,但我得到了同样的结果。

当我查看谷歌文档时,我发现了这个:

重要提示: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源代码中看到'product'下有一个OAuth 2.0库,但我不知道如何使用它。另外,我对Auth 2.0有点紧张,因为我读过的内容并不是非常互补,而且似乎更容易在脚下拍摄(可能没有根据,但它似乎是一个反复出现的主题)。

对于Google+,我找到了 这些说明 这看起来很简单,但差不多一年前,所以我想知道这是否仍然是最好的方式。我也找到了 这个git存储库 实施Google oauth2。不过,我想知道这是否仍然具有相关性,因为它是从前一段时间开始的。

所以,问题是 - 在asp.net mvc5中使用DotNetOpenAuth与谷歌的最佳/官方方式是什么?希望我没有错过任何明显的东西,在这种情况下只需指向一些链接即可。

更新 我找到了这个  和这个  哪些是相关的。我想我会用git google auth2,除非我被告知。

解析度

我做了以下事情: -

  • 按照接受的答案提供的链接中的步骤操作。它是 这个链接

重要的是在登录后继续使用SSL而不是回退到HTTP,您的登录cookie就像您的用户名和密码一样秘密...在您登录后重定向回HTTP不会使当前请求或将来的请求更快。

  • 在Nuget上获得最新的DotNetOpenAuth.GoogleOAuth2。

  • 我看了一下这个推荐 这个msdn博客 (由同一作者)关于如何最好地保护网站。基本上,建议添加以下强制所有页面为HTTPS的内容:

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

最终这意味着整个网站都是HTTPS。自从进行这些更改后,该网站运行良好。


5618
2018-01-29 00:53


起源

你好。是否可以从MVC 4使用OAuth 2进行Google身份验证?或者我应该首先将我的项目升级到MVC 5?您是否遵循任何指导方针进行此类升级?谢谢! - Jaime
@Jaime - 我升级到MVC 5来执行此操作,同时保留在Visual Studio 2012上。我不记得确切的步骤,但我很确定我遵循了这些网站中的说明: asp.net/mvc/overview/releases/...  stackoverflow.com/questions/19102831/...  blogs.msdn.com/b/webdev/archive/2013/11/18/...。 - acarlon
谢谢@acarlon,我在VS2013上。我已经迁移了应用程序,但没有成功。尝试调试时出现HTTP 500错误。我试图找出它缺少或没有正确配置。 - Jaime


答案:


以下是使用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”,那么您可能在第一次创建解决方案时创建了一个“空白”项目,而不是“Internet”项目。当您第一次开始时,选择“互联网应用程序”会更容易(如果计划使用外部登录)。不确定您使用的编辑器,但Visual Studio 2012/2013使这个变得非常容易!

如果您打算使用现在推荐的OpenID方式,这是一个很好的起点: https://developers.google.com/accounts/docs/OpenID#settingup

最后,如果您可以通过编辑器(Visual Studio)访问NUGET,您将发现这些任务,例如添加oAuth-1/2或openId已经变得非常简单。

如果以上内容并不适合您的构建,这里有一个最后一个链接会让您朝着正确的方向前进......有了更多细节,我非常乐意帮助您找到最佳解决方案。我可以说的一件事是,oauth2仍然非常相关并且在今天的许多应用程序中使用,并且在今天开始一个新项目时实现这一点并没有错 - 这将是正确的方式(或至少一个正确的方法去... ...希望这有一些帮助,而不仅仅是沿着你已经失败的道路前进。

希望一切安好。


5
2018-01-29 06:15



啊,谢谢。我不知何故错过了这个链接。这比我原先计划的要容易。 - acarlon
没问题!这些东西曾经是如此微不足道。即使只是在过去2年左右的时间里,.NET已经走过了漫长的道路,这样的事情开箱即用。通过这种方式,我们可以专注于“用户可以通过门后的所有解决方案和业务逻辑;”很高兴我能提供帮助! - Battery1


这就是您使用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 );
                }
            }
        }

9
2018-01-29 07:50



谢谢,我会用这个作为参考。我认为battery1提供的链接具有我需要的大部分信息,因此我将以此作为答案。 - acarlon
@acarlon:没问题。但请注意,您的问题是关于DotnetOpenAuth的,我的答案正是如此。 - Wiktor Zychla
同意这就是为什么我+1。由于mvc5方法此时看起来更简单,我将继续使用它。 - acarlon
这个解决方案适用于ASP.NET MVC 4吗?或者是否需要升级到MVC 5?谢谢! - Jaime
@Jaime:它适用于所有地方,MVC 4,5,WebForms。 - Wiktor Zychla