我有一个.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();
}还有一个回调页面(比方说一个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做点什么?(我试着简明扼要,不写我试过的所有东西,但如果需要,我可以提供更多的信息)
发布于 2016-02-16 21:54:37
如果您使用的是4.5或更高版本的using,如许多网站blogpost所述,如果您使用Visual 2013+模板创建了一个2013+项目,那么您将有一个如何实现它的示例就像这里提到的。或者您可以使用简单易用的IdentityModel。
对于.NET 4.0,我最终使用了DotNetOpenAuth,找到如何调用库并不是一种容易的方法,所以我将分享我的发现,这是获得用户授权的第一步id。
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();
}使用GetAuthServerDescription构建一个AuthorizationServerDescription
回调url的方法是一个简单的post (获取令牌),因为我没有找到如何向提供者发送所需的确切参数。
https://stackoverflow.com/questions/35326131
复制相似问题