Combine the use of authentication both for MVC pages and for Web API pages?

后端 未结 5 1504
面向向阳花
面向向阳花 2020-12-12 21:52

I have an MVC 5 web application and can login with a Login.cshtml page and get a cookie and the login works fine. But, I would like to do a login with the Web API and then (

5条回答
  •  悲哀的现实
    2020-12-12 22:17

    I assume what you're trying to do is have pages served by MVC have javascript that makes calls to Web API methods. If you're using ASP.NET Identity to handle authentication (which it looks like you're doing), then MVC should be using OAuth tokens that can be passed to Web API for authentication.

    Here's a snippet from some javascript code that works for me in a similar situation:

    var token = sessionStorage.getItem('access_token');
    var headers = {};
    if (token) {
        headers.Authorization = 'Bearer ' + token;
    }
    $.ajax({
        type: ,
        url: ,
        headers: headers
    }).done(function (result, textStatus) {
    

提交回复
热议问题