MVC 5身份验证和HttpClient没有可用的MediaTypeFormatter



我有一个MVC 5 Web Api和一个连接到它的桌面应用程序

我有一个控制器,并从应用程序连接到它,没有任何问题,但当我在控制器中放入[Authorize]属性时,桌面应用程序中的httpclient停止工作,并表示没有可用的MediaTypeFormatter。

我认为HttpClient运行良好,我尝试使用请求头定义身份验证:httpClient.DefaultRequestHeaders.Authorization并且使用这种方式CCD_ 4。

在使用MVC 4的过程中,它与身份验证一起工作。

在这种情况下,使用MVC 5,它在使用Anonymous时工作得很好,但当我进行身份验证时,我在httpresponse.Content.ReadAsAsync中得到了这个异常

No MediaTypeFormatter is available to read an object of type 'Dictionary`2' from content with media type 'text/html'.

我该去哪里看?

更新

我将httpResponse作为字符串读取,以查看httpclient得到了什么,并在字符串中读取登录页面的HTML。身份验证失败,MVC+neneneba API重定向到登录页面。我现在该怎么办?为什么身份验证不能像以前的MVC 4 web API那样工作?是因为新的OWIN身份验证吗?

这些问题是因为身份验证不起作用。MVC5使用Web表单身份验证,所以我必须创建一种新的方式来验证HttpClient。

  • 进入/Account/Login页面并阅读_RequestVerificationToken
  • 张贴到/帐户/登录用户名、密码和_RequestVerificationToken

错误是因为当身份验证失败时,MVC5会将HttpClient重定向到登录页。所以我得到的不是JSON,而是HTML。登录页面的HTML。

尝试:

httpClient.Accept="text/html,application/xhtml+xml,application/json";

这篇文章应该会有所帮助。

http://www.asp.net/web-api/overview/security/basic-authentication

它告诉我们,我们需要实现Web API的身份验证处理程序。

我认为Web API在VS2013中是一个独立的项目类型,请确保您没有使用默认使用表单身份验证的MVC。

如果你必须使用整个MVC包,那么你必须像Ricardo Polo所说的那样,制作一个http post到登录页面,然后将你得到的cookie附加到每个api调用。例如:

HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(string.Format("https://{0}/login", server));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
bool loginSuccess = false;
if (response.StatusCode == HttpStatusCode.OK)
{
//If there's a cookie in the response, it means login successful, 
//and the cookie is the one returned by the server that can be used to identify
//the client when calling Authorized api calls.
loginSuccess = (response.Cookies.Count > 0);
}
response.Close();
//Now make a request to the api
HttpWebRequest ApiRequest = (HttpWebRequest)System.Net.WebRequest.Create(string.Format("https://{0}/api/values", server));
ApiRequest.CookieContainer = new CookieContainer();
ApiRequest.CookieContainer.Add(response.Cookies[0]);
//From here add code to get the response.

上面的代码可以使用HttpClient完成。

我有一个例外,当我的Action输入参数是Dictionary时,我通过创建自定义模型来解决它,该模型保存了我所有的输入参数和Dictionary,之后它就可以工作了

Visual Studio中的MVC 5 Web API(Web API 2)模板使用基于令牌的OWIN中间件进行身份验证。

阅读以下文章:

身份验证并获取承载令牌http://www.asp.net/vnext/overview/authentication/individual-accounts-in-aspnet-web-api

In the Fiddler Composer tab, enter the following URL (remember to change "port" to the actual port number):
http://localhost:port/Token
Leave the request method as POST. Change the Content-Type header to application/x-www-form-urlencoded. Replace the request body with the following:
grant_type=password&username=Alice&password=password123
Click Execute to send the request. Here is the raw HTTP request:
POST http://localhost:49436/Token HTTP/1.1
User-Agent: Fiddler
Host: localhost:49436
Content-Type: application/x-www-form-urlencoded
Content-Length: 55
grant_type=password&username=Alice&password=password123

在MVC 5 Web API模板中,您可以启用基于cookie的身份验证,但要获得Authenticated,您需要通过发送具有三个字段1的表单主体的HttpPost消息来使用/Token端点。grant_type 2。用户名3。暗语

响应此消息,您将获得一个JSON对象,您可以在其中找到access_token(如果您不想使用基于令牌的授权,则可以忽略该对象),如果配置了使用cookie身份验证(默认),则服务器将设置一个cookie。默认情况下,从ApiControllers派生的控制器在某些VS模板中不使用cookie身份验证。您需要存储access_token,并将其放置在向ApiController的所有进一步请求的标头中。Http标头应如下所示:Authorization: Bearer [place_accesstoken_without_sorrunding_braces]

相关内容

  • 没有找到相关文章

最新更新