问题描述
我正在使用 Azure AD 构建一个应用来调用 Microsoft Graph.在某些需要提升访问权限的请求中,图表会发出 HTTP 403 错误,其中包含我需要在后续请求中使用的 WWW-Authenticate
标头中的特殊 claims
参数.
I'm building an app using Azure AD to call the Microsoft Graph. In certain requests that require elevated access, the graph is issuing an HTTP 403 error with a special claims
parameter inside the WWW-Authenticate
header I need to use in a subsequent request.
在 .NET 中,如何提取 WWW-Authenticate API 为响应来自 HttpResponseMessage
类的 Forbidden (HTTP 403) 而生成的标头?
In .NET, how can I extract the WWW-Authenticate header produced by an API in response to a Forbidden (HTTP 403) from the HttpResponseMessage
class?
此外,解析此标头以提取某些数据的最佳方法是什么?例如,响应以逗号分隔,但在我需要的数据块中也包含逗号.
Moreover, what's the best way to parse this header to extract out certain pieces of data? For instance, the response is comma separated, but also contains commas inside the chunk of data I need.
推荐答案
为了提取参数,可以使用以下代码提取WWW-Authenticate
头:
In order to extract the parameter, you can use the following code to extract the WWW-Authenticate
header:
HttpResponseMessage graphResponse = await httpClient.SendAsync(request);
graphResponse.Headers.WwwAuthenticate.ToString();
这将提供整个标题.不提取声明参数,您可以通过 ,
和后面的空格来解析 WWW-Authenticate
标头.HTTP 的 RFC 没有给出明确的指导,因此它基于单个服务.对于这个特定的错误,用逗号和空格分隔或通过查找 claims
是合适的.
This will provide the entire header. Not to extract the claims parameter, you can parse the WWW-Authenticate
header by ,
and a space after. The RFC for HTTP does not give clear guidance so it's based on the individual service. For this particular error, it splitting by a comma and space or by looking for claims
is appropriate.
这篇关于从 .NET 中的 HttpResponseMessage 中提取和解析 WWW-Authenticate 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!