使用授权类型作为密码获取基于用户的访问令牌
Fetching user based access token using grant type as password
我正在尝试使用用户凭据检索访问令牌。
我正在使用 AcquireTokenAsync 方法检索令牌,我正在使用构造函数将资源、客户端 ID 和用户凭据作为参数。
public async Task<IHttpActionResult> GetToken()
{
AuthenticationResult authenticationResult = null;
try
{
string authority = "https://login.microsoftonline.com/tenant";
string resource ="2424-234-234234-234-23-32423";
string username = "yxyzzz";
string password = "password";
string clientId="2424-234-234234-234-23-32423";
var useridpassword = new UserPasswordCredential(username, password);
AuthenticationContext context = new AuthenticationContext(authority);
context.TokenCache.Clear();
authenticationResult = await context.AcquireTokenAsync(resource, clientId, useridpassword);
return authenticationResult.AccessToken;
}
catch (Exception ex)
{
throw ex;
}
}
我期待返回访问令牌,但我在获取令牌时遇到异常。以下是我收到的错误消息。
AdalException: {"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: 674f29fe-73c6-49a3-9c3f-24df4ea16000\r\nCorrelation ID: b14cb535-9df5-48fa-b911-7e8b927fceb7\r\nTimestamp: 2019-11-08 06:21:57Z","error_codes":[7000218],"timestamp":"2019-11-08 06:21:57Z","trace_id":"674f29fe-73c6-49a3-9c3f-24df4ea16000","correlation_id":"b14cb535-9df5-48fa-b911-7e8b927fceb7","error_uri":"https://login.microsoftonline.com/error?code=7000218"}: Unknown error
要使用资源所有者密码凭证,您需要将应用程序视为 public 客户端。
转到 Azure 门户 -> 应用程序注册 -> 找到你的应用程序 -> 检查高级设置
这是我用来获取令牌的代码。这就是我想要检索访问令牌的内容。
string authority = "https://login.microsoftonline.com/tenant";
string resource ="2424-234-234234-234-23-32423";
string username = "yxyzzz";
string password = "password";
string clientId="2424-234-234234-234-23-32423";
string tokenEndpointUri = authority + "/oauth2/token";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("client_id", authmodel.ClientId),
new KeyValuePair<string, string>("client_secret", authmodel.ClientSecret),
new KeyValuePair<string, string>("resource", resource)
}
);
using (var client = new HttpClient())
{
HttpResponseMessage res = null;
client.PostAsync(tokenEndpointUri, content).
ContinueWith(t =>
{
try
{
res = t.Result;
}
catch (Exception ex)
{
throw ex;
}
})
.Wait();
string json = await res.Content.ReadAsStringAsync();
}
我正在 json 变量中获取访问令牌以及其他详细信息。如果你想获取token的值,那么你可以将它反序列化为.net对象并获取值。
我正在尝试使用用户凭据检索访问令牌。
我正在使用 AcquireTokenAsync 方法检索令牌,我正在使用构造函数将资源、客户端 ID 和用户凭据作为参数。
public async Task<IHttpActionResult> GetToken()
{
AuthenticationResult authenticationResult = null;
try
{
string authority = "https://login.microsoftonline.com/tenant";
string resource ="2424-234-234234-234-23-32423";
string username = "yxyzzz";
string password = "password";
string clientId="2424-234-234234-234-23-32423";
var useridpassword = new UserPasswordCredential(username, password);
AuthenticationContext context = new AuthenticationContext(authority);
context.TokenCache.Clear();
authenticationResult = await context.AcquireTokenAsync(resource, clientId, useridpassword);
return authenticationResult.AccessToken;
}
catch (Exception ex)
{
throw ex;
}
}
我期待返回访问令牌,但我在获取令牌时遇到异常。以下是我收到的错误消息。
AdalException: {"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: 674f29fe-73c6-49a3-9c3f-24df4ea16000\r\nCorrelation ID: b14cb535-9df5-48fa-b911-7e8b927fceb7\r\nTimestamp: 2019-11-08 06:21:57Z","error_codes":[7000218],"timestamp":"2019-11-08 06:21:57Z","trace_id":"674f29fe-73c6-49a3-9c3f-24df4ea16000","correlation_id":"b14cb535-9df5-48fa-b911-7e8b927fceb7","error_uri":"https://login.microsoftonline.com/error?code=7000218"}: Unknown error
要使用资源所有者密码凭证,您需要将应用程序视为 public 客户端。
转到 Azure 门户 -> 应用程序注册 -> 找到你的应用程序 -> 检查高级设置
这是我用来获取令牌的代码。这就是我想要检索访问令牌的内容。
string authority = "https://login.microsoftonline.com/tenant";
string resource ="2424-234-234234-234-23-32423";
string username = "yxyzzz";
string password = "password";
string clientId="2424-234-234234-234-23-32423";
string tokenEndpointUri = authority + "/oauth2/token";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("client_id", authmodel.ClientId),
new KeyValuePair<string, string>("client_secret", authmodel.ClientSecret),
new KeyValuePair<string, string>("resource", resource)
}
);
using (var client = new HttpClient())
{
HttpResponseMessage res = null;
client.PostAsync(tokenEndpointUri, content).
ContinueWith(t =>
{
try
{
res = t.Result;
}
catch (Exception ex)
{
throw ex;
}
})
.Wait();
string json = await res.Content.ReadAsStringAsync();
}
我正在 json 变量中获取访问令牌以及其他详细信息。如果你想获取token的值,那么你可以将它反序列化为.net对象并获取值。