加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用Web Api验证.NET MVC应用程序

发布时间:2020-12-16 06:49:59 所属栏目:asp.Net 来源:网络整理
导读:我有一个基于SPA VS 2013模板的Web Api 2项目.我在该Api中配置了承载令牌认证. 我还有一个单独的MVC 5项目,我想使用该Web Api进行身份验证.那可能吗?怎么样? 到目前为止我做了什么(在我的Mvc客户端): using (var client = new HttpClient()){ client.Base
我有一个基于SPA VS 2013模板的Web Api 2项目.我在该Api中配置了承载令牌认证.

我还有一个单独的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)
      {
           //
      }
}

它得到了令牌,但现在呢?

解决方法

如果你得到了令牌,你应该全部设置好.您只需要在每个请求的标题中提供它,例如:

Authorization: Bearer boQtj0SCGz2GFGz[...]

编辑:

使用HttpClient,您可以执行以下操作:

var requestMessage = new HttpRequestMessage(HttpMethod.Post,"http://localhost/MyApi/");
requestMessage.Headers.Add("Authorization","Bearer boQtj0SCGz2GFGz[...]");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读