Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 5身份验证和HttpClient没有可用的MediaTypeFormatter_C#_Asp.net Mvc_Authentication_Asp.net Web Api_Asp.net Mvc 5 - Fatal编程技术网

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

C# MVC 5身份验证和HttpClient没有可用的MediaTypeFormatter,c#,asp.net-mvc,authentication,asp.net-web-api,asp.net-mvc-5,C#,Asp.net Mvc,Authentication,Asp.net Web Api,Asp.net Mvc 5,我有一个MVC5WebAPI和一个连接到它的桌面应用程序 我有一个控制器,从应用程序连接到它时没有任何问题,但当我将[Authorize]属性放入控制器时,桌面应用程序中的httpclient停止工作,并表示没有可用的MediaTypeFormatter 我认为HttpClient工作正常,我尝试使用请求头定义身份验证: httpClient.DefaultRequestHeaders.Authorization 并使用这种方式httpclient=newhttpclient(newhttpcl

我有一个MVC5WebAPI和一个连接到它的桌面应用程序

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

我认为HttpClient工作正常,我尝试使用请求头定义身份验证:
httpClient.DefaultRequestHeaders.Authorization
并使用这种方式
httpclient=newhttpclient(newhttpclienthandler{Credentials=newnetworkcredential(“用户名”、“密码”)})

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

在本例中,使用MVC 5时,它可以完美地工作,但是当我进行身份验证时,我在
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+API重定向到登录页面。 我现在该怎么办? 为什么身份验证不像以前的MVC4WebAPI那样工作?是因为新的OWIN身份验证吗?

请尝试:


httpClient.Accept=“text/html,application/xhtml+xml,application/json”

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

  • 进入/Account/Login页面并阅读_RequestVerificationToken
  • Post to/Account/Login用户名、密码和_RequestVerificationToken

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

本文应该会有所帮助

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

我认为WebAPI在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完成。

当我的操作输入参数是Dictionary时,我有一个例外,我通过创建保存所有输入参数和Dictionary的自定义模型来解决它,然后它就开始工作了。

MVC5 Web API(Web API 2)VisualStudio中的模板使用基于令牌的OWIN中间件进行身份验证

阅读以下文章:

验证并获取承载令牌

在MVC 5 Web API模板中,您可以启用基于cookie的身份验证,但要获得身份验证,您需要使用/Token端点,方法是发送一条Http Post消息,表单正文包含三个字段1。grant_类型2。用户名3。密码

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

我没有从web服务器读取字符串。我正在阅读字典或对象…我添加了httpclient.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“application/json”))。仍然没有MediaTypeFormatter可用于从媒体类型为“text/html”的内容中读取类型为“Dictionary”“2”的对象。在Web Api中,默认情况下返回的是JSON。从您在文章中提到的详细信息来看,服务器似乎返回了一个错误,可能是html文档中的详细信息,这就是响应内容类型为
text/html
的原因。默认情况下,Web API附带用于
json
xml
formurlencoded
的格式化程序,ReadAsync也只理解这些格式…试试这个..执行
httpresponse.Content.ReadAsStringAsync()。结果
获取html并检查您看到的错误..@KiranChalla您太棒了!我根据您的建议更新我的答案您是否在进行身份验证时存储cookie,并在需要进行身份验证时将其传递给服务器?
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