我已經創建的Web API,但是當我嘗試使用控制檯應用程序,我收到以下錯誤註冊到它:不能調用身份驗證的WebAPI的任何方法,即使登錄或註冊認證
response = {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { X-SourceFiles: =?UTF-8?B?RDpcU1RFQ0hfUHJvamVjdFxQcm9qZWN0c1xMb3lhbHR5XFNhbnN1dGVjaC5MdHkuRGF0YS5XZWIuQXBpXGFwaVxBY2NvdW50XFJlZ2lzdGVy?= Cache-Control: private Server: Microsoft-IIS/8.0 WWW-Authenticate: Bearer, Negotiate, NTLM X-Powered-By: ASP.NET
如何我可以使用控制檯客戶端註冊到API嗎?
我可以打電話從postman
註冊方法並獲得註冊到應用
這裏是我的控制檯代碼通之前註冊
static async Task<string> PostRequest4(string token, string apiBaseUri, string requestPath, RegisterBindingModel t)
{
HttpClientHandler hndlr = new HttpClientHandler();
hndlr.UseDefaultCredentials = true;
using (var client = new HttpClient())
{
//setup client
client.BaseAddress = new Uri("http://localhost:38811/");
client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//make request
HttpResponseMessage response = await client.PostAsJsonAsync(requestPath, t);
//make request
Stream stream = await response.Content.ReadAsStreamAsync();
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(ResponseMessage));
ResponseMessage objResponse = (ResponseMessage)dataContractJsonSerializer.ReadObject(stream);
string res = "";
if (objResponse == null)
{
res = "error occurred!";
}
else
{
if (objResponse.ErrorMessage != null)
{
res = "error occurred!";
}
else if (objResponse.IsSuccess)
{
res = "Item ID " + objResponse.ID.ToString() + " have been added Successfully!";
}
}
return res;
}
}
如果我添加以下行,然後我就可以成功註冊到應用程序
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
在這裏我通過以下方法使用現有用戶「用戶名和密碼」得到令牌
private static async Task<string> GetAPIToken(string userName, string password, string apiBaseUri)
{
using (var client = new HttpClient())
{
//setup client
client.BaseAddress = new Uri(apiBaseUri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//setup login data
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", userName),
new KeyValuePair<string, string>("password", password),
});
//send request
HttpResponseMessage responseMessage = await client.PostAsync(Token , formContent);
//get access token from response body
var responseJson = await responseMessage.Content.ReadAsStringAsync();
var jObject = JObject.Parse(responseJson);
return jObject.GetValue("access_token").ToString();
}
}
我想打電話給註冊方法不涉及
我會很感激,如果有人可以幫助我現有的用戶(用戶名和密碼)。
〜謝謝〜
是您的註冊方法或它的控制器上裝飾有'[授權]'屬性?使用'Register'操作方法頂部的'[AllowAnonymous]'屬性嘗試註釋。 –
感謝您的回覆,我已經嘗試過。並且[System.Web.Http.AllowAnonymous]但仍然相同, –
您是否也檢查過'[Authorize]'?我想最可能的問題是授權只基於您發佈的錯誤消息。還要檢查令牌驗證在應用程序中的發生情況,以及它是否正在驗證「Register」方法。 –