C# DotNetOpenAuth OAuth2
因此,我已经进入了OAuth2的第一步,并设法使用从私有API获取访问令牌。因为这只是整个过程中的一小步,我想知道是否有一个框架可以简化过程并管理令牌 我在很多博客上都读过,我已经花了一些时间阅读了文档,但出于某种原因,我不明白如何使用它来实现客户机和某种“令牌管理器/处理器” 有谁能给我提供一个小样本,或者至少列出OAuth2客户机和“令牌管理器/处理程序”所必需的类 谢谢大家!C# DotNetOpenAuth OAuth2,c#,oauth-2.0,dotnetopenauth,C#,Oauth 2.0,Dotnetopenauth,因此,我已经进入了OAuth2的第一步,并设法使用从私有API获取访问令牌。因为这只是整个过程中的一小步,我想知道是否有一个框架可以简化过程并管理令牌 我在很多博客上都读过,我已经花了一些时间阅读了文档,但出于某种原因,我不明白如何使用它来实现客户机和某种“令牌管理器/处理器” 有谁能给我提供一个小样本,或者至少列出OAuth2客户机和“令牌管理器/处理程序”所必需的类 谢谢大家! 编辑 这是一个“令牌”类,基本上只存储请求访问令牌时从授权服务器接收的数据: public class Toke
编辑 这是一个“令牌”类,基本上只存储请求访问令牌时从授权服务器接收的数据:
public class Token
{
public Token()
{
Issued = DateTime.Now;
}
[JsonProperty("access_token")]
public string AccessToken
{
get;
set;
}
[JsonProperty("token_type")]
public string TokenType
{
get;
set;
}
// ...
}
Dictionary<string, string> authCred =
Configuration.
GetSection("Authentication:Credentials").
GetChildren().
Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).
ToDictionary(x => x.Key, x => x.Value);
var client = new RestClient(Configuration["tokenurl"]);
RestRequest request = new RestRequest()
{
Method = Method.POST
};
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{authCred["client-id"]}:{authCred["client-secret"]}")));
request.AddParameter("application/x-www-form-urlencoded", $"grant_type={authCred["grant-type"]}&scope={authCred["scope"]}", ParameterType.RequestBody);
var response = client.Execute(request);
Token test = JsonConvert.DeserializeObject<Token>(response.Content);
appsettings.json
{
"Authentication": {
"tokenurl": "https://tokenurl.com/give/me/token",
"Credentials": {
"client-id": "clientid",
"client-secret": "clientsecret",
"grant-type": "client_credentials",
"scope": "scope"
}
}
这就是我请求访问令牌的地方:
public class Token
{
public Token()
{
Issued = DateTime.Now;
}
[JsonProperty("access_token")]
public string AccessToken
{
get;
set;
}
[JsonProperty("token_type")]
public string TokenType
{
get;
set;
}
// ...
}
Dictionary<string, string> authCred =
Configuration.
GetSection("Authentication:Credentials").
GetChildren().
Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).
ToDictionary(x => x.Key, x => x.Value);
var client = new RestClient(Configuration["tokenurl"]);
RestRequest request = new RestRequest()
{
Method = Method.POST
};
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{authCred["client-id"]}:{authCred["client-secret"]}")));
request.AddParameter("application/x-www-form-urlencoded", $"grant_type={authCred["grant-type"]}&scope={authCred["scope"]}", ParameterType.RequestBody);
var response = client.Execute(request);
Token test = JsonConvert.DeserializeObject<Token>(response.Content);
Dictionary authCred=
配置
GetSection(“身份验证:凭据”)。
GetChildren()。
选择(x=>newkeyvaluepair(x.Key,x.Value))。
ToDictionary(x=>x.Key,x=>x.Value);
var client=new RestClient(配置[“tokenurl]”);
RestRequest请求=新的RestRequest()
{
Method=Method.POST
};
AddHeader(“内容类型”、“应用程序/json”);
request.AddHeader(“Authorization”,Convert.ToBase64String(System.Text.ascienceoding.ASCII.GetBytes($”{authCred[“client id”]}:{authCred[“client secret”]}));
request.AddParameter(“application/x-www-form-urlencoded”,“$”grant_type={authCred[“grant type”]}&scope={authCred[“scope”]}”,ParameterType.RequestBody);
var response=client.Execute(请求);
令牌测试=JsonConvert.DeserializeObject(response.Content);
这实际上效果很好
我的问题是,我是否必须自己构建一个处理此令牌的逻辑,或者是否已经有一个框架为我完成所有工作(我已经阅读了DotNetOpenAuth,但还没有找到一个对我有帮助的示例)?欢迎使用Stackoverflow!请提供一个代码的工作示例和一个更具体的问题。也许你可以看看。