rad*_*rad 2 c# .net-4.0 dotnetopenauth oauth-2.0 owin
我有一个.NET 4.0应用程序,我需要添加一个第三层的OAuth2身份验证。我有点困惑(很难找到.NET 4.0的示例和文档)。
我可以将Microsoft.AspNet.Membership.OpenAuth(OAuth 2.0)与Asp.net 4.0(.NET 4.0)一起使用吗?
我的另一个选择是使用DotNetOpenAuth,但是在.NET 4.0中找到带有Webforms回调的示例时遇到了一些麻烦。
据我了解,我应该有一个身份验证页面(登录页面):
var medOK = new WebServerClient(GetAuthServerDescription(), clientIdentifier: "some client id");
medOK.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("some secret code");
// CallBack
var state = new AuthorizationState();
var uri = Request.Url.AbsoluteUri;
uri = RemoveQueryStringFromUri(uri);
state.Callback = new Uri(uri);
var accessTokenResponse = medOK.ProcessUserAuthorization();
if (accessTokenResponse != null){
//If you have accesstoek then do something
} else if (this.AccessToken == null) {
// If we don't yet have access, immediately request it.
medOK.PrepareRequestUserAuthorization(state);
medOK.RequestUserAuthorization();
}
Run Code Online (Sandbox Code Playgroud)
还有一个回调页面(例如ashx页面):
var medOK = new WebServerClient(GetAuthServerDescription(), clientIdentifier: "some client id");
medOK.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("some secret code");
var response = medOK.GetClientAccessToken();
// Then I get claims
Run Code Online (Sandbox Code Playgroud)
有感觉吗?(我尽量简洁,不写所有尝试的内容,但是如果需要,我可以提供更多信息)
如果您使用的是4.5或更高版本,请按照许多网站博客文章中的描述使用Owin,并且如果使用Visual Studio 2013+模板创建Asp.net项目,则将有一个示例来实现它,如此处所述。或者,您可以使用简单易用的IdentityModel。
对于.NET 4.0,我结束使用DotNetOpenAuth,这不是找到如何调用该库的简便方法,因此,我将分享我的发现,这是获得用户授权的第一步。
var client = new WebServerClient(GetAuthServerDescription(), clientIdentifier: "client id");
client.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("secrete");
// CallBack
uri = RemoveQueryStringFromUri(callBack);
state.Callback = new Uri(uri);
var accessTokenResponse = client.ProcessUserAuthorization();
if (accessTokenResponse != null){
//If you have accesstoek then do something
} else {
// If we don't yet have access, immediately request it.
client.PrepareRequestUserAuthorization(state).Send();
}
Run Code Online (Sandbox Code Playgroud)
使用GetAuthServerDescription构建AuthorizationServerDescription
回调URL的方法是一个简单的文章(获取令牌),因为我没有找到如何发送需要发送给提供程序的确切参数。