How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.NET Web API 的 Azure AD OAuth2 访问令牌和刷新令牌)
问题描述
我已经实现了 Azure AD OAuth2 守护程序或服务器到 ASP.NET Web API.但是,我只收到一个访问令牌,它是 AuthenticationResult 上的属性.请参阅下面的实现.
public IHttpActionResult GetAccessToken(string clientId, string clientkey){AuthenticationContext authContext = new AuthenticationContext(authority);ClientCredential clientCredential = new ClientCredential(clientId, clientkey);AuthenticationResult authenticationResult = authContext.AcquireTokenAsync(resourceUri, clientCredential).Result;授权授权=新授权{access_token = authenticationResult.AccessToken,token_type = authenticationResult.AccessTokenType,expires_on = authenticationResult.ExpiresOn };返回确定(授权);}这仅返回访问令牌.我想要一个实现,一个守护进程或服务器实现,它返回访问令牌和刷新令牌.您是否看过或做过类似的实现.欢迎任何有用的示例链接.
当我发布这个问题时,这就是我正在寻找的答案,请参阅下面的屏幕截图以了解预期结果和 c# 控制台解决方案.找到解决方案后,值得在这里分享,可能有一天对某人有用
在下面的邮递员屏幕截图中实现预期结果的 C# 控制台应用程序代码
使用系统;使用 System.Collections.Generic;使用 System.Net.Http;命名空间 AzureADTokenApp{课堂节目{静态无效主要(字符串 [] 参数){var client = new HttpClient();var uri = "https://login.microsoftonline.com/<tenant-name>.onmicrosoft.com/oauth2/token?api-version=1.0";var 对 = 新列表<KeyValuePair<字符串,字符串>>{new KeyValuePair<string, string>("resource", "https://graph.microsoft.com"),new KeyValuePair("client_id", "I have implemented an Azure AD OAuth2 Daemon or Server to ASP.NET Web API.
However I only receive an access token which is the property on the AuthenticationResult. See implementation below.
public IHttpActionResult GetAccessToken(string clientId, string clientkey)
{
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, clientkey);
AuthenticationResult authenticationResult = authContext.AcquireTokenAsync(resourceUri, clientCredential).Result;
Authorisation authorisation = new Authorisation {access_token = authenticationResult.AccessToken,
token_type = authenticationResult.AccessTokenType,
expires_on = authenticationResult.ExpiresOn };
return Ok(authorisation);
}
This returns only access token. I would like an implementation, a Daemon or Server implementation that returns both access token and refresh token. Have your seen or done similar implementation. Any useful links to an example are welcome.
解决方案 When I posted this question, this was the answer I was looking for, please see screen shot below for expected result and c# console solution.
Having found the solution, it is worth sharing it here, may be useful to someone some day
C# console app code to achieve expected result in the postman screen shot below
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace AzureADTokenApp
{
class Program
{
static void Main(string[] args)
{
var client = new HttpClient();
var uri = "https://login.microsoftonline.com/<tenant-name>.onmicrosoft.com/oauth2/token?api-version=1.0";
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("resource", "https://graph.microsoft.com"),
new KeyValuePair<string, string>("client_id", "<azure ad client id e.g. 9b864-a5e6-4f0d-b155-1f53a6c78>"),
new KeyValuePair<string, string>("client_secret", "<azure ad client secret e.g. MTMiXaO1P9HnhSawdXWmcnuQ="),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "<azure ad user e.g. julius.depulla@example.com>"),
new KeyValuePair<string, string>("password", "<azure ad user password e.g. Pa$$word01>"),
new KeyValuePair<string, string>("scope", "openid")
};
var content = new FormUrlEncodedContent(pairs);
var response = client.PostAsync(uri, content).Result;
string result = string.Empty;
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
}
Console.WriteLine(result);
Console.ReadLine();
}
}
}
Screenshot from Postman - Expected Result. You will have same result in console except is less readable
这篇关于如何获取守护进程或服务器到 C# ASP.NET Web API 的 Azure AD OAuth2 访问令牌和刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取守护进程或服务器到 C# ASP.NET Web API 的
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
