【问题标题】:Implement OAuth2 with Asp.net 4.0使用 Asp.net 4.0 实现 OAuth2
【发布时间】:2016-05-21 10:47:56
【问题描述】:

我有一个 .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 中使用 Callback for 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

有意义吗? (我尽量简洁,不写所有我试过的,但如果需要我可以提供更多信息)

【问题讨论】:

    标签: c# .net-4.0 oauth-2.0 owin dotnetopenauth


    【解决方案1】:

    如果您使用 4.5 或更高版本,请按照许多网站博客文章中的说明使用 Owin,并且如果您使用 Visual Studio 2013+ 模板创建 Asp.net 项目,您将有一个如何实现它的示例,like mentioned here。或者你可以使用简单易用的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 的方法是一个简单的帖子(获取令牌),因为我没有找到如何发送我需要发送给我的提供者的确切参数。

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2014-05-11
      • 1970-01-01
      相关资源
      最近更新 更多