发送已验证的请求


                    

我有一个使用asp.net身份保护的MVC项目:</ p>

这是我的登录功能:</ p>

  self.login = function(){
     event.preventDefault();
     如果(!$('#formLogin')。valid()){
         返回false;
     }
     var loginData = {
         grant_type:“密码”,
         用户名:self.userName(),
         密码:self.password()
     };
     $ .ajax({
         输入:“ POST”,
         网址:“ / API /令牌”,
         数据:loginData
     })。done(函数(数据){
         //将访问令牌缓存在会话存储中。
         sessionStorage.setItem(tokenKey,data.access_token);
         self.authenticate();
         //将登录按钮的状态更改为注销
         self.redirect('Users');
     })。fail(showError);
}

self.authenticate = function(){
     self.token = sessionStorage.getItem(tokenKey);
     var headers = {};
     console.log(self.token);
     如果(self.token){
         headers.Authorization ='Bearer'+ self.token;
     }
     $ .ajaxSetup({
         标头:标头
     });
}
</ code> </ pre>

效果很好,我成功获得了 token </ code>,并且标头设置正确。</ p>

问题是,当我尝试发送请求时-例如:</ p>

  self.getUsers = function(){
     $ .get(“ / API / Users / GetUsers /”);
}
</ code> </ pre>

我从服务器收到401错误:</ p>


  

“消息”:“此请求的授权已被拒绝。” </ p>
</ blockquote>

我在做什么错了?</ p>
     </ div>

展开原文

原文

I have an MVC project secured with a asp.net identity:

This is my Login function:

self.login = function () {
    event.preventDefault();
    if (!$('#formLogin').valid()) {
        return false;
    }
    var loginData = {
        grant_type: 'password',
        username: self.userName(),
        password: self.password()
    };
    $.ajax({
        type: 'POST',
        url: '/API/Token',
        data: loginData
    }).done(function (data) {
        // Cache the access token in session storage.
        sessionStorage.setItem(tokenKey, data.access_token);
        self.authenticate();
        //change status of Login button to Logout
        self.redirect('Users');
    }).fail(showError);
}

self.authenticate = function () {
    self.token = sessionStorage.getItem(tokenKey);
    var headers = {};
    console.log(self.token);
    if (self.token) {
        headers.Authorization = 'Bearer ' + self.token;
    }
    $.ajaxSetup({
        headers: headers
    });
}

That works fine, I get the token successfully and the headers are set up correctly.

The problem is that when I try to send a request- for example:

self.getUsers = function () {
    $.get("/API/Users/GetUsers/");
}

I get a 401 error from the server:

"message": "Authorization has been denied for this request."

What am I doing wrong?

1个回答


根据 jQuery.ajax <的官方文档</ a>, / code>,使用它来设置每个请求的自定义标头:</ p>

  $。ajaxSetup({
     beforeSend:function(xhr){
         xhr.setRequestHeader('Authorization','...');
     }
});
</ code> </ pre>
     </ div>

展开原文

原文

According to the official documentation of the jQuery.ajax, use this to set custom headers of each request:

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', '...');
    }
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐