我有一个基于 SPA VS 2013 模板的 Web Api 2 项目。我在该 Api 中配置了不记名 token 身份验证。
我还有一个单独的 MVC 5 项目,我想使用该 Web Api 进行身份验证。那可能吗?如何?
到目前为止我所做的(在我的 Mvc 客户端中):
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost/MyApi/");
var response = client.PostAsync("Token", new StringContent("grant_type=password&username=teste&password=123456", Encoding.UTF8)).Result;
if (response.IsSuccessStatusCode)
{
//
}
}
它得到了 token ,但现在怎么办?
最佳答案
如果你得到 token ,你应该一切都准备好了。您只需要在每个请求的 header 中提供它,例如:
Authorization: Bearer boQtj0SCGz2GFGz[...]
编辑:
使用 HttpClient 你会做这样的事情:
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost/MyApi/");
requestMessage.Headers.Add("Authorization", "Bearer boQtj0SCGz2GFGz[...]");
关于asp.net-mvc - 使用 Web Api 验证 .NET MVC 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22516747/