我是Azure的新手,在学习Azure Active Directory(AAD)的所有功能方面做了一些努力,所以我希望你能为我清除一些东西。这是我已经做过的事情:
现在我想做什么:
我想编写一个在我的构建服务器上运行的应用程序来测试用户权限。该应用程序必须使用C#.NET Core编写。现在我正在努力研究如何从我的代码以用户身份登录,所以我的问题是:
如何以用户身份从代码登录到AAD并获取JWT令牌来测试用户权限?我可以通过使用用户名/密码来完成此操作,还是需要在AAD中注册我的测试应用程序?达到目标的最佳解决方案是什么?
先感谢您
Juunas的评论已涵盖了大部分要求。只是在它背后添加一些细节。
一些措辞可以帮助你:
https://<yourtenant>.onmicrosoft.com/<yourapi>/user_impersonation
https://<clientId-of-API>/.default
...https://login.microsoftonline.com/yourtenant.onmicrosoft.com
来自wiki的密码授权的代码示例(更多示例):
static async Task GetATokenForGraph()
{
string authority = "https://login.microsoftonline.com/contoso.com";
string[] scopes = new string[] { "user.read" };
PublicClientApplication app = new PublicClientApplication(clientId, authority);
try
{
var securePassword = new SecureString();
foreach (char c in "dummy") // you should fetch the password
securePassword.AppendChar(c); // keystroke by keystroke
result = await app.AcquireTokenByUsernamePasswordAsync(scopes, "[email protected]",
securePassword);
}
catch(MsalException)
{
// See details below
}
Console.WriteLine(result.Account.Username);
}
我实际上在没有使用MSAL库的情况下找到了在“纯”C#中实现它的方法,我遇到了一些问题。因此,如果您正在寻找没有MSAL的解决方案,您可以按照下面描述的方式进行。
先决条件
C#代码
你可以创建一个类“JwtFetcher”并使用这样的代码:
public JwtFetcher(string tenantId, string clientId, string resource)
{
this.tenantId = !string.IsNullOrEmpty(tenantId) ? tenantId : throw new ArgumentNullException(nameof(tenantId));
this.clientId = !string.IsNullOrEmpty(clientId) ? clientId : throw new ArgumentNullException(nameof(clientId));
this.resource = !string.IsNullOrEmpty(resource) ? resource : throw new ArgumentNullException(nameof(resource));
}
public async Task<string> GetAccessTokenAsync(string username, string password)
{
var requestContent = this.GetRequestContent(username, password);
var client = new HttpClient
{
BaseAddress = new Uri(ApplicationConstant.Endpoint.BaseUrl)
};
var message = await client.PostAsync(this.tenantId + "/oauth2/token", requestContent).ConfigureAwait(false);
message.EnsureSuccessStatusCode();
var jsonResult = await message.Content.ReadAsStringAsync().ConfigureAwait(false);
dynamic objectResult = JsonConvert.DeserializeObject(jsonResult);
return objectResult.access_token.Value;
}
private FormUrlEncodedContent GetRequestContent(string username, string password)
{
List<KeyValuePair<string, string>> requestParameters = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>(ApplicationConstant.RequestParameterName.GrantType, ApplicationConstant.RequestParameterValue.GrantTypePassword),
new KeyValuePair<string, string>(ApplicationConstant.RequestParameterName.Username, username),
new KeyValuePair<string, string>(ApplicationConstant.RequestParameterName.Password, password),
new KeyValuePair<string, string>(ApplicationConstant.RequestParameterName.ClientId, this.clientId),
new KeyValuePair<string, string>(ApplicationConstant.RequestParameterName.Resource, this.resource)
};
var httpContent = new FormUrlEncodedContent(requestParameters);
return httpContent;
}
此授权类型只是“密码”。