NET中的HttpResponseMessage中提取和解析WW

NET中的HttpResponseMessage中提取和解析WW

本文介绍了从.NET中的HttpResponseMessage中提取和解析WWW-Authenticate头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure AD构建应用程序来调用Microsoft Graph。在某些需要提升访问权限的请求中,图表在 WWW-Authenticate claim 参数c> header我需要在后续请求中使用。

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中,如何提取标题由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 header:

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();

这将提供整个标头。要不提取claim参数,您可以通过和之后的空格解析 WWW-Authenticate 标题。 HTTP的RFC没有提供明确的指导,因此它基于单个服务。对于此特定错误,它以逗号和空格分隔或通过查找声明是合适的。

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头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:56