Asp.net mvc 4 DotNetOpenAuth 4.3和Google-openid2.0+;OAuth 1.0已弃用
如果你想切入正题,问题是:在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,除非别人告诉我 分辨率 我做了以下工作:-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,但我得到了相同的结果 当我查看谷歌文档时,我发现: 重要
- 按照接受答案提供的链接中的步骤进行操作。是的李>
- 在Nuget上获得了最新的DotNetOpenAuth.GoogleOAuth2
- 我看了(同一作者)关于如何最好地保护网站的建议。基本上,建议添加以下内容,这将强制所有页面使用HTTPS:
filters.Add(new System.Web.Mvc.requireHttpAttribute())代码>
这最终意味着整个站点都是HTTPS。自从做了这些改变之后,这个网站一直运行良好 以下是使用谷歌认证以及其他一些社交整合的推荐方式: 为了使用oauth2(假设您使用的是MVC)
希望一切顺利。这就是如何将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方法现在看起来更简单,我就用它。这个解决方案对你有用吗