rac*_*cha 10 c# authentication azure-active-directory adal .net-core
在ADAL.Net 3.x中,UserPasswordCredential在2.x的UserCredential之上引入.但同一个UserPasswordCredential在同一个nuget包下的.Net Core中没有公开?
UserCredential类只有一个属性UserName
namespace Microsoft.IdentityModel.Clients.ActiveDirectory
{
//
// Summary:
// Credential used for integrated authentication on domain-joined machines.
public class UserCredential
{
//
// Summary:
// Constructor to create user credential. Using this constructor would imply integrated
// authentication with logged in user and it can only be used in domain joined scenarios.
public UserCredential();
//
// Summary:
// Constructor to create credential with client id and secret
//
// Parameters:
// userName:
// Identifier of the user application requests token on behalf.
public UserCredential(string userName);
//
// Summary:
// Gets identifier of the user.
public string UserName { get; }
}
}
Run Code Online (Sandbox Code Playgroud)
由于.NetCore中没有UserPasswordCredential,而UserCredential只使用一个参数用户名,如何输入用户的密码并在.Net Core中实现以下代码?
authContext.AcquireTokenAsync(WebAPIResourceId, ClientId, userPasswordCredential);
Run Code Online (Sandbox Code Playgroud)
我在.Net Core 1.0版本中特别使用ADAL 3.13.4版本
Fei*_*SFT 13
要使用资源所有者密码凭据授予流来获取Azure AD的访问令牌,我们可以使用HttpClient来调用http请求.以下是供您参考的示例:
HttpClient client = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/{tenantId}/oauth2/token";
var body = "resource={resourceUrl}&client_id={clientId}&grant_type=password&username={userName}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
var result=await client.PostAsync(tokenEndpoint, stringContent).ContinueWith<string>((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
});
JObject jobject = JObject.Parse(result);
var token = jobject["access_token"].Value<string>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5441 次 |
| 最近记录: |