首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用OAuth2 4.0实现OAuth2

用OAuth2 4.0实现OAuth2
EN

Stack Overflow用户
提问于 2016-02-10 21:20:18
回答 1查看 9.1K关注 0票数 2

我有一个.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找到回调示例时遇到了一些困难。

据我理解,我应该有一个身份验证页面(登录页面):

代码语言:javascript
复制
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页面),其中包含:

代码语言:javascript
复制
var medOK = new WebServerClient(GetAuthServerDescription(), clientIdentifier: "some client id");
medOK.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("some secret code");
var response = medOK.GetClientAccessToken();

// Then I get claims

做点什么?(我试着简明扼要,不写我试过的所有东西,但如果需要,我可以提供更多的信息)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-16 21:54:37

如果您使用的是4.5或更高版本的using,如许多网站blogpost所述,如果您使用Visual 2013+模板创建了一个2013+项目,那么您将有一个如何实现它的示例就像这里提到的。或者您可以使用简单易用的IdentityModel

对于.NET 4.0,我最终使用了DotNetOpenAuth,找到如何调用库并不是一种容易的方法,所以我将分享我的发现,这是获得用户授权的第一步id。

代码语言:javascript
复制
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 (获取令牌),因为我没有找到如何向提供者发送所需的确切参数。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35326131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档