如何使用 OAuth 2 - OAuth 2 C# 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14876126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:18:11  来源:igfitidea点击:

How to use OAuth 2 - OAuth 2 C# example

c#oauthoauth-2.0dotnetopenauth

提问by MonsterMMORPG

I have to figure out how to use OAuth 2 in order to use Deviantart api.

我必须弄清楚如何使用 OAuth 2 才能使用 Deviantart api。

I got the client_id and client_secret part

我得到了 client_id 和 client_secret 部分

Here the information they give

他们提供的信息在这里

Endpoints

端点

The only information you need to authenticate with us using OAuth 2.0 are the client_idand client_secretvalues for your app, as well as the endpoint shown below.

您使用 OAuth 2.0 向我们进行身份验证所需的唯一信息是您的应用程序的client_idclient_secret值,以及如下所示的端点。

OAuth 2.0 draft 10:

OAuth 2.0 草案 10:

https://www.deviantart.com/oauth2/draft10/authorizehttps://www.deviantart.com/oauth2/draft10/token

https://www.deviantart.com/oauth2/draft10/authorize https://www.deviantart.com/oauth2/draft10/token

OAuth 2.0 draft 15:

OAuth 2.0 草案 15:

https://www.deviantart.com/oauth2/draft15/authorizehttps://www.deviantart.com/oauth2/draft15/token

https://www.deviantart.com/oauth2/draft15/authorize https://www.deviantart.com/oauth2/draft15/token

Placebo call

安慰剂通话

The first API call relying on OAuth 2.0 authentication is the placebo call. It's useful for checking that an access token is still valid before making a real API call that might be long, like a file upload. You call it with one of the following endpoints (an access token must be provided):

第一个依赖 OAuth 2.0 身份验证的 API 调用是安慰剂调用。在进行可能很长的真实 API 调用(如文件上传)之前,检查访问令牌是否仍然有效非常有用。您可以使用以下端点之一调用它(必须提供访问令牌):

https://www.deviantart.com/api/draft10/placebohttps://www.deviantart.com/api/draft15/placebo

https://www.deviantart.com/api/draft10/placebo https://www.deviantart.com/api/draft15/placebo

You need to use the endpoint that corresponds to the OAuth 2.0 draft you've obtained your token with.

您需要使用与您获得令牌的 OAuth 2.0 草案相对应的端点。

It always returns the following JSON: {status: "success"}

它始终返回以下 JSON: {status: "success"}

I have searched the web and found this awesome library.

我在网上搜索并找到了这个很棒的图书馆。

DotNetOpenAuth v4.0.1

DotNetOpenAuth v4.0.1

http://www.dotnetopenauth.net/

http://www.dotnetopenauth.net/

Added it as reference but have no idea what to do next. Even a very small example would be really useful about how to use OAuth 2

添加它作为参考,但不知道下一步该怎么做。即使是一个很小的例子也会对如何使用 OAuth 2 非常有用

using DotNetOpenAuth;
using DotNetOpenAuth.OAuth2;

Here the page where deviantart gives the information

这是 deviantart 提供信息的页面

http://www.deviantart.com/developers/oauth2

http://www.deviantart.com/developers/oauth2

Ok here what i got so far but not working

好的,我到目前为止得到了什么,但没有工作

public static WebServerClient CreateClient() {
    var desc = GetAuthServerDescription();
    var client = new WebServerClient(desc, clientIdentifier: "myid");
    client.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("mysecret");
    return client;
}

public static AuthorizationServerDescription GetAuthServerDescription() {
    var authServerDescription = new AuthorizationServerDescription();
    authServerDescription.AuthorizationEndpoint = new Uri(@"https://www.deviantart.com/oauth2/draft15/authorize");
    authServerDescription.TokenEndpoint = new Uri(@"https://www.deviantart.com/oauth2/draft15/token");
    authServerDescription.ProtocolVersion = ProtocolVersion.V20;
    return authServerDescription;
}

采纳答案by Iain Galloway

Easiest thing to do now is get Visual Studio 2013 and create a new ASP.NET Web Application choosing "Individual User Accounts" as your authentication type. There's a working OAuth 2 implementation out of the box in there (configured at App_Start\Startup.Auth.cs) which you can slice out and then adapt to your needs.

现在最简单的事情是获取 Visual Studio 2013 并创建一个新的 ASP.NET Web 应用程序,选择“个人用户帐户”作为您的身份验证类型。那里有一个开箱即用的 OAuth 2 实现(在 App_Start\Startup.Auth.cs 中配置),您可以将其切出然后适应您的需求。

回答by Tolbxela

In the ASP.NET Core Security Project there is now a ready to use solution:

在 ASP.NET Core 安全项目中,现在有一个现成的解决方案:

Nuget Package: AspNet.Security.OAuth.DeviantArt

Nuget 包:AspNet.Security.OAuth.DeviantArt