Asp.net mvc 4 DotNetOpenAuth 4.3和Google-openid2.0+;OAuth 1.0已弃用

Asp.net mvc 4 DotNetOpenAuth 4.3和Google-openid2.0+;OAuth 1.0已弃用,asp.net-mvc-4,dotnetopenauth,asp.net-mvc-5,Asp.net Mvc 4,Dotnetopenauth,Asp.net Mvc 5,如果你想切入正题,问题是:在asp.net mvc 5中,与Google一起使用DotNetOpenAuth的最佳/官方方式是什么 大约一年前,我使用了OAuth(DotNetOpenAuth OAuth和OpenID),就像在asp.NETMVC4中一样(就像在示例项目中一样)。从那时起,我成功地将它用于谷歌、facebook、雅虎和微软。然而,最近我遇到了很多用户登录谷歌。我曾尝试升级到MVC 5和DotNetOpenAuth 4.3,但我得到了相同的结果 当我查看谷歌文档时,我发现: 重要

如果你想切入正题,问题是:在asp.net mvc 5中,与Google一起使用DotNetOpenAuth的最佳/官方方式是什么

大约一年前,我使用了OAuth(DotNetOpenAuth OAuth和OpenID),就像在asp.NETMVC4中一样(就像在示例项目中一样)。从那时起,我成功地将它用于谷歌、facebook、雅虎和微软。然而,最近我遇到了很多用户登录谷歌。我曾尝试升级到MVC 5和DotNetOpenAuth 4.3,但我得到了相同的结果

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

重要提示:谷歌已经不赞成对OAuth 1.0的支持。如果你是 使用OpenID2.0+OAuth1.0,我们建议您切换到Google+ 登录。Google+登录提供OAuth 2.0身份验证 具有丰富社交功能的机制,并可访问额外的Google 桌面和移动功能。它支持所有Google用户和 透明迁移。有关详细信息,请参阅谷歌的迁移 认证

我很可能错了,因为我认为现成的asp.net mvc 4 DotNetOpenAuth使用OpenID2.0(我使用MinimumRequiredOpenVersion=“V20”)+OAuth 1.0。我可以在DotNetOpenAuth源代码中看到“产品”下有一个OAuth 2.0库,但我不知道如何使用它。另外,我对auth2.0有点紧张,因为我读到的内容不是很互补,而且似乎更容易击中自己的脚(可能没有根据,但这似乎是一个反复出现的主题)

对于Google+,我发现这似乎很简单,但那几乎是一年前的事了,所以我想知道这是否仍然是最好的方式。我还发现了实现GoogleOAuth2。不过,我想知道这是否仍然是相关的,因为这一切都是从一段时间以前开始的

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

更新 我发现这个和这个是相关的。我想我会选择git上的GoogleAuth2,除非别人告诉我

分辨率

我做了以下工作:-

  • 按照接受答案提供的链接中的步骤进行操作。是的
重要的是在登录后继续使用SSL,而不是退回到HTTP,您的登录cookie与您的用户名和密码一样保密…登录后重定向回HTTP不会使当前请求或将来的请求更快

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

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

    filters.Add(new System.Web.Mvc.requireHttpAttribute())


这最终意味着整个站点都是HTTPS。自从做了这些改变之后,这个网站一直运行良好

以下是使用谷歌认证以及其他一些社交整合的推荐方式:

为了使用oauth2(假设您使用的是MVC)

  • 启用Google OpenID提供程序 打开App_Start\Startup.Auth.cs文件并删除//App.UseGoogleAuthentication()中的注释字符;启用Google身份验证

  • 在“使用其他服务登录”下,单击谷歌。然后用户被重定向到google站点,您将在那里输入您的凭据

  • 如果您没有“app_start”这个文件或文件夹,那么您可能创建了一个“blank”项目,而不是第一次创建解决方案时的“internet”项目。当您第一次开始时,选择“internet应用程序”要容易得多(如果计划使用外部登录)。不确定您使用的编辑器是什么,但VisualStudio2012/2013让这变得非常简单

    如果您打算使用OpenID,这是现在推荐的方法,这里有一个很好的起点:

    最后,如果您可以通过编辑器(如Visual studio)访问NUGET,您会发现添加oAuth-1/2或openId等任务变得非常简单

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


    希望一切顺利。

    这就是如何将DotnetOpenAuth与Google/OAuth2结合使用

    首先,从Nuget中引用DotnetOpenAuth.Ultimate包

    然后创建一个provider类和profile模型类

    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 );
                    }
                }
            }
    

    啊,谢谢。我不知怎么错过了那个环节。这应该比我原来计划的要容易。没问题!这些事情过去要琐碎得多。NET已经走过了很长的一段路,即使是在过去的两年左右,像这样的东西都是很好的开箱即用。这样我们就可以集中精力“用户进门后的所有解决方案和业务逻辑;)很高兴我能帮忙!谢谢,我会用这个作为参考。我认为Batter1提供的链接包含了我所需要的大部分信息,所以我将以此作为答案。@acarlon:没问题。但是请注意,您的问题是专门针对DotnetOpenAuth的,我的回答正好解决了这个问题。我同意这就是为什么我+1。既然mvc5方法现在看起来更简单,我就用它。这个解决方案对你有用吗