gpt4 book ai didi

asp.net-core - Azure Devops Oauth 身份验证 : Cannot get access token (BadRequest Failed to deserialize the JsonWebToken object)

转载 作者:行者123 更新时间:2023-12-05 05:04:02 26 4
gpt4 key购买 nike

我正在尝试为 Azure Devops 的自定义 Web 应用程序实现 OAUth 2.0 流程。我正在关注这个 https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops文档以及此 https://github.com/microsoft/azure-devops-auth-samples/tree/master/OAuthWebSample OauthWebSample 但使用 ASP.NET Core(我还阅读了一个关于 SO 的问题,它看起来很相似但不是:Access Azure DevOps REST API with oAuth)

复制

我已经在 https://app.vsaex.visualstudio.com/app/register 注册了一个 azdo 应用程序并且授权步骤似乎工作正常,即用户可以授权应用程序并且重定向到我的应用程序返回看起来像有效 jwt token 的东西:

header: {
"typ": "JWT",
"alg": "RS256",
"x5t": "oOvcz5M_7p-HjIKlFXz93u_V0Zo"
}
payload: {
"aui": "b3426a71-1c05-497c-ab76-259161dbcb9e",
"nameid": "7e8ce1ba-1e70-4c21-9b51-35f91deb6d14",
"scp": "vso.identity vso.work_write vso.authorization_grant",
"iss": "app.vstoken.visualstudio.com",
"aud": "app.vstoken.visualstudio.com",
"nbf": 1587294992,
"exp": 1587295892
}

下一步是获取访问 token ,该 token 因BadReqest: invalid_client, Failed to deserialize the JsonWebToken object而失败。

这里是完整的例子:

public class Config
{
public string ClientId { get; set; } = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
public string Secret { get; set; } = "....";
public string Scope { get; set; } = "vso.identity vso.work_write";
public string RedirectUri { get; set; } = "https://....ngrok.io/azdoaccount/callback";
}

/// <summary>
/// Create azdo application at https://app.vsaex.visualstudio.com/
/// Use configured values in above 'Config' (using ngrok to have a public url that proxies to localhost)
/// navigating to localhost:5001/azdoaccount/signin
/// => redirect to https://app.vssps.visualstudio.com/oauth2/authorize and let user authorize (seems to work)
/// => redirect back to localhost:5001/azdoaccount/callback with auth code
/// => post to https://app.vssps.visualstudio.com/oauth2/token => BadReqest: invalid_client, Failed to deserialize the JsonWebToken object
/// </summary>
[Route("[controller]/[action]")]
public class AzdoAccountController : Controller
{
private readonly Config config = new Config();
[HttpGet]
public ActionResult SignIn()
{
Guid state = Guid.NewGuid();

UriBuilder uriBuilder = new UriBuilder("https://app.vssps.visualstudio.com/oauth2/authorize");
NameValueCollection queryParams = HttpUtility.ParseQueryString(uriBuilder.Query ?? string.Empty);

queryParams["client_id"] = config.ClientId;
queryParams["response_type"] = "Assertion";
queryParams["state"] = state.ToString();
queryParams["scope"] = config.Scope;
queryParams["redirect_uri"] = config.RedirectUri;

uriBuilder.Query = queryParams.ToString();

return Redirect(uriBuilder.ToString());
}

[HttpGet]
public async Task<ActionResult> Callback(string code, Guid state)
{
string token = await GetAccessToken(code, state);
return Ok();
}

public async Task<string> GetAccessToken(string code, Guid state)
{
Dictionary<string, string> form = new Dictionary<string, string>()
{
{ "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" },
{ "client_assertion", config.Secret },
{ "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" },
{ "assertion", code },
{ "redirect_uri", config.RedirectUri }
};

HttpClient httpClient = new HttpClient();

HttpResponseMessage responseMessage = await httpClient.PostAsync(
"https://app.vssps.visualstudio.com/oauth2/token",
new FormUrlEncodedContent(form)
);
if (responseMessage.IsSuccessStatusCode) // is always false for me
{
string body = await responseMessage.Content.ReadAsStringAsync();
// TODO parse body and return access token
return "";
}
else
{
// Bad Request ({"Error":"invalid_client","ErrorDescription":"Failed to deserialize the JsonWebToken object."})
string content = await responseMessage.Content.ReadAsStringAsync();
throw new Exception($"{responseMessage.ReasonPhrase} {(string.IsNullOrEmpty(content) ? "" : $"({content})")}");
}
}
}

最佳答案

当请求访问 token 时,必须为 client_assertion 参数提供 Client Secret 而不是 App Secret:

enter image description here

关于asp.net-core - Azure Devops Oauth 身份验证 : Cannot get access token (BadRequest Failed to deserialize the JsonWebToken object),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com