简体   繁体   English

如何使用JQuery AJAX使用HTTP Basic Auth阻止Firefox提示用户名/密码?

[英]How do I keep Firefox from prompting for username/password with HTTP Basic Auth with JQuery AJAX?

I'm writing some browser side dynamic functionality and using HTTP Basic Auth to protect some resources. 我正在编写一些浏览器端动态功能,并使用HTTP Basic Auth来保护一些资源。 The user experience is very important and is highly customized. 用户体验非常重要,并且高度定制。

Here's a simple test JQuery method that eventually will test if a user has supplied the right credentials in a form: 这是一个简单的测试JQuery方法,最终将测试用户是否在表单中提供了正确的凭据:

$(document).ready(function() {
    $("#submit").click(function() {
    var token = Base64.encode($('#username').val() + ':' + $('#password').val());        
    $.ajax({
      url: '/private',
      method: 'GET',
      async: false,
      beforeSend: function(req) {
        req.setRequestHeader('Authorization', 'test:password');
      },
      error: function(request, textStatus, error) {
        if (request.status == 401) {
          alert('401');
        }
      }
    });
    return false;
  });
});

If they are not allowed to access /private , at the moment they should see just the alert box. 如果他们不被允许访问/private ,那么他们应该只看到警报框。 However, on Firefox, a browser-provided login form pops up (to retry with new credentials). 但是,在Firefox上,会弹出一个浏览器提供的登录表单(使用新凭据重试)。 Safari does not do this. Safari不会这样做。

We want to completely control the experience with custom forms, fades, transitions, etc. How can I keep Firefox's default box from being shown? 我们希望通过自定义表单,淡入淡出,转换等完全控制体验。如何防止Firefox的默认框显示? (If this will be an issue when we test for IE, I'd love to hear solutions there, too.) (如果我们测试IE时会出现这个问题,我也很乐意听到那里的解决方案。)

The solution is to set the WWW-Authenticate header to something other than Basic . 解决方案是将WWW-Authenticate标头设置为Basic以外的其他标头。 For example set it to: 例如,将其设置为:

WWW-Authenticate: None

or 要么

WWW-Authenticate: FormBased

if you use form based login. 如果您使用基于表单的登录。 Then the browser will not show you a login window. 然后浏览器不会显示登录窗口。

Unfortunatly, I am hitting the same issue here. 不幸的是,我在这里遇到同样的问题。

In my opinion, Browsers should not give a prompt for an xmlhttprequest. 在我看来,浏览器不应该提示xmlhttprequest。 I really wish someone would push that cause people are really wanting to move to jQuery for their auth needs. 我真的希望有人会推动这一点,因为人们真的想要转向jQuery以满足他们的身份验证需求。

Well here is the help I can give you, I found this jQuery Digest thing, I have no idea what it really does or anything, but if someone could take this code the right way, we could have a jquery digest auth system. 那么这里是我能给你的帮助,我发现了这个jQuery Digest的东西,我不知道它真正做了什么或者什么,但是如果有人能够以正确的方式使用这个代码,我们可以有一个jquery digest auth系统。

https://www.openhub.net/p/digestj https://www.openhub.net/p/digestj

I would think with this handy new AuthDigestDomain option, we could have the above script rewritten or whatever and have the secured area 'linked' together and we could get past this problem once and for all. 我想用这个方便的新的AuthDigestDomain选项,我们可以重写上面的脚本或者其他任何东西并将安全区域“链接”在一起,我们可以一劳永逸地解决这个问题。 Well... best of luck =) 嗯...祝你好运=)

In case you haven't read it: 如果你还没看过:

How can I supress the browser's authentication dialog? 如何抑制浏览器的身份验证对话框?

Doesn't look too promising :) 看起来不太有希望:)

I found somewhere a workaround of this authentication popup issue. 我找到了这个身份验证弹出问题的解决方法。

WWW-Authenticate: None

doesn't work for me, but I've added 不适合我,但我补充说

'Authorization': 'Basic'

to the headers and it works like a charm. 到标题,它就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从javascript获取HTTP Basic Auth用户名? - Get the HTTP Basic Auth username from javascript? jQuery Ajax Post继续在URL上添加用户名和密码 - jQuery Ajax Post keep adding username and password on the URL 如何通过JQuery / Ajax验证用户名/密码? - How to validate a username / password via JQuery / Ajax? 如何使用angular进行HTTP基本身份验证 - How to do HTTP Basic Auth using angular 通过Ajax进行基本身份验证,无需在对话框中输入用户名和密码即可访问使用https保护的页面 - Basic Auth through Ajax for accessing a page secured with https without entering Username and Password in dialog box 如何在adapter-impl js文件中以用户名和密码以及auth类型发送基本授权 - how to send the basic authorization with username and password and auth type as basic in adapter-impl js file XMLHttpRequest没有在Firefox中缓存基本的HTTP身份验证 - XMLHttpRequest not caching basic HTTP auth in firefox jquery ajax&preemptive basic auth - jquery ajax & preemptive basic auth 如何从浏览器中删除“授权:基本用户名:密码”标题 - How to remove 'Authorization: Basic username:password' header from browser jQuery $ .getJSON具有用户名和密码的基本身份验证? - JQuery $.getJSON with username and password basic authentication?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM