最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get Authorization from HTTP-Request header - Stack Overflow

programmeradmin4浏览0评论

I already searched within SO for some threads about this, but could only find some which explained what this header is for or how to get the authorization header in c# but I don't want to read it from server side but from client side.

Is there any way to get the Base64 encoded header "Authorization" from the browser? I want to implement a tool where you can log in and if you click on a spezific button your username will be saved.

My problem is that the browser does the authorization automatically, and with jQuery and JavaScript methods you can only set the requestheaders and get the responseheaders. I couldn't find a method to get the requestheaders.

The library gethttp could get some headers, but not the authorization header. My guess is that this header is hidden.

I'm doing a login via SVN and the browser does the authorization the moment you enter the website.

Only the username is enough. I'm searching for solutions where the user doesn't have to input their username.

I already searched within SO for some threads about this, but could only find some which explained what this header is for or how to get the authorization header in c# but I don't want to read it from server side but from client side.

Is there any way to get the Base64 encoded header "Authorization" from the browser? I want to implement a tool where you can log in and if you click on a spezific button your username will be saved.

My problem is that the browser does the authorization automatically, and with jQuery and JavaScript methods you can only set the requestheaders and get the responseheaders. I couldn't find a method to get the requestheaders.

The library gethttp could get some headers, but not the authorization header. My guess is that this header is hidden.

I'm doing a login via SVN and the browser does the authorization the moment you enter the website.

Only the username is enough. I'm searching for solutions where the user doesn't have to input their username.

Share Improve this question edited Oct 3, 2014 at 23:32 VKen 5,0045 gold badges33 silver badges43 bronze badges asked Jul 28, 2014 at 8:26 mantimanti 2411 gold badge2 silver badges9 bronze badges 8
  • 3 You're trying to read that header using C# in the server-side or using Javascript in the client-side? – haim770 Commented Jul 28, 2014 at 8:28
  • uh forgot to say this..js on the client side – manti Commented Jul 28, 2014 at 8:28
  • have you tried .getAllResponseHeaders() method in XHR object? – Anto Subash Commented Jul 28, 2014 at 8:34
  • 3 You should never trust the browser, it's like the first rule of web club, come on! – php_nub_qq Commented Jul 28, 2014 at 8:47
  • 2 well it's just for use within intranet so it shouldn't be that big of a problem – manti Commented Jul 28, 2014 at 8:51
 |  Show 3 more comments

3 Answers 3

Reset to default 5

I'm assuming you're trying to use the Basic Realm authorisation mechanism This had already been replied on Stackoverflow and involves the $.ajax() jquery object.
How to use Basic Auth with jQuery and AJAX?
So please don't upvote me on this

$.ajaxSetup({
  headers: {
    'Authorization': "Basic XXXXX"
  },
  data: '{ "comment" }',
  success: function (){
    alert('Thanks for your comment!'); 
  }
});

where XXXXX is your username:password base64 encoded

You can use native fetch API:

fetch("http://localhost:8888/validate",{
  method:"GET",
  headers: {"Authorization": "Bearer xxxxx"}
})
.then(res => res.json())
.then(
  (result) => {
    // do something
  },
  // Note: it's important to handle errors here
  // instead of a catch() block so that we don't swallow
  // exceptions from actual bugs in components.
  (error) => {
    // handle error
  }
)

It's not possible to get the headers for the request of the CURRENT page. This has been asked several times on SO.

However, you can make a new request and retrieve the headers of that request. That way you are able to get the Basic Auth headers, base64 decode that string and then you have the username (and also the password).

Decoding base64 in javascript can be done using the following function as suggested by @michael in the comments.

window.atob("base64encodedString");
发布评论

评论列表(0)

  1. 暂无评论