我希望用户能够通过HTTP基本身份验证模式登录.
问题是我还希望他们能够再次注销 - 奇怪的是,浏览器似乎并不支持.
这被认为是一种社交黑客风险 - 用户将其机器解锁并且浏览器打开,而其他人可以轻松地访问该网站.请注意,仅关闭浏览器选项卡不足以重置令牌,因此用户可能很容易错过.
所以我想出了一个解决方法,但这是一个完全的问题:
1)将它们重定向到Logoff页面
2)在该页面上激活一个脚本到ajax加载另一个带有伪凭证的页面:
$j.ajax({
url: '<%:Url.Action("LogOff401", new { id = random })%>',
type: 'POST',
username: '<%:random%>',
password: '<%:random%>',
success: function () { alert('logged off'); }
});
Run Code Online (Sandbox Code Playgroud)
3)应该总是第一次返回401(强制传递新凭据),然后只接受伪凭证:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOff401(string id)
{
// if we've been passed HTTP authorisation
string httpAuth = this.Request.Headers["Authorization"];
if (!string.IsNullOrEmpty(httpAuth) &&
httpAuth.StartsWith("basic", StringComparison.OrdinalIgnoreCase))
{
// build the string we expect - don't allow regular users to pass
byte[] enc = Encoding.UTF8.GetBytes(id + ':' + id);
string expected …
Run Code Online (Sandbox Code Playgroud) 我正在编写.Net中的客户端应用程序,它通过HTTP与服务器通信.
在NTLM和Kerberos授权的情况下,我需要设置不同的请求缓冲选项.
如何确定是否使用NTLM或Kerberos?有可能以某种方式解码'WWW-Authenticate:Negotiate'标题?
如果您希望传达用户未登录,即使登录机制是基于表单而不是基于HTTP(Basic,Digest等),也可以为AJAX调用的响应返回HTTP 401状态?
这里的答案表明应该使用401:https: //stackoverflow.com/a/6937030/2891365
这篇文章展示了一个使用401进行AJAX响应的人的实际例子:http://www.bennadel.com/blog/2228-some-thoughts-on-handling-401-unauthorized-errors-with-jquery.htm
但是,HTTP/1.1的RFC 2616明确指出需要特殊的头,这意味着它只能用于HTTP身份验证.
10.4.2 401未经授权
该请求需要用户身份验证.响应必须包含一个
WWW-Authenticate
标题字段(第14.47节),其中包含适用于所请求资源的质询.
我想我可能会发送一个虚假的标题WWW-Authenticate: WebForm
,但仍然符合W3C规范,但感觉它违反了WWW-Authenticate
标题的精神.
最后,我似乎无法找到明确说明是否允许HTTP 401响应的权威来源.我错过了这方面的权威来源吗?
authentication ajax http www-authenticate http-response-codes
我已经实现了一个使用基本身份验证(使用 spring 安全性)的 Web 服务器。
我在访问 URL 时禁用了默认身份验证入口点(而不是使用 www-authentication 标头响应 401,它只返回 401),目的是防止浏览器显示身份验证弹出窗口。
我能够使用 javascript 代码和 curl 等命令行工具连接到服务器,但是当我使用浏览器(chrome 和 firefox)对其进行测试时,它们只是不发送标头。
curl -v -u user:password localhost:8080/user
GET /user HTTP/1.1
Host: localhost:8080
Authorization: Basic dXNlcjpwYXNzd29yZA==
User-Agent: curl/7.58.0
Accept: /
Chrome:版本 71.0.3578.98(官方版本)(64 位)
http://user:password@localhost:8080/user
GET /user HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ 71.0.3578.98 Safari/537.36
DNT: 1
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng, / ;q=0.8
Accept-Encoding: gzip, deflate, br 接受语言:en-AU,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-GB;q=0.6,en-US;q=0.5
为什么浏览器不发送身份验证标头。
在我的akka-http应用程序中,我从另一个响应WWW-Authenticate
头部的安全服务返回响应:当akka-http解析此头部然后将WWW-Authenticate
值呈现为字符串时,其中一个参数的引号丢失.我在渲染中遗漏了什么,或者这是一个akka-http错误?
这是一个测试用例:
import akka.http.scaladsl.model.HttpHeader
import akka.http.scaladsl.model.HttpHeader.ParsingResult
import akka.http.scaladsl.model.headers.{`WWW-Authenticate`, HttpChallenge}
import org.scalatest.{Matchers, WordSpec}
class wwwAuthenticateParserTest extends WordSpec with Matchers {
"WWW-Authenticate header" should {
"have correctly-quoted strings" in {
val rawHeader = HttpHeader.parse("WWW-Authenticate",
"Bearer error=\"invalid_request\", error_description=\"Access token is not found\"")
val expectedHeader = `WWW-Authenticate`(HttpChallenge("Bearer",
"",
Map("error" -> "invalid_request", "error_description" -> "Access token is not found")))
rawHeader match {
case ParsingResult.Ok(`WWW-Authenticate`(challenges), _) =>
challenges.head should be(expectedHeader.challenges.head)
challenges.head.params("error") should be("invalid_request")
challenges.head.toString should be("Bearer realm=\"\",error=\"invalid_request\",error_description=\"Access token is not found\"")
case …
Run Code Online (Sandbox Code Playgroud) 微软利用我目前所知的两个WWW-Authenticate新增功能
如果从服务器发送Negotiate,则基于一组条件将使用Kerberos
然后将在服务器和客户端之间尝试Kerberos,如果不满足上述条件,则将尝试NTLM.
我的问题是,是否有一些方法让服务器指示不应该发送NTLM?我目前通过跟踪会话中的请求来处理这个问题,如果收到NTLM消息,它会在其余的会话生命周期中禁用Kerberos和WWW-Authenticate.
HttpClient
我想通过我自己的 C# API向外部 API 发送请求。我正在使用 dot net 5 和基本身份验证。这是我的代码:
var client = new HttpClient
{
BaseAddress = new Uri(baseUrl)
};
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Put, "apiUrl");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var param = JsonConvert.SerializeObject(new
{
param1="",
param2=""
});
requestMessage.Content = new StringContent(param, Encoding.UTF8, "application/json");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{user}:{pass}")));
HttpResponseMessage response = await client.SendAsync(requestMessage);
Run Code Online (Sandbox Code Playgroud)
通常,我像这样发送http请求。但现在我遇到了一些问题。
从我的请求标头中删除行授权标头后HttpResponseMessage response = await client.SendAsync(requestMessage);
,我收到 UnAuthorized http 错误。
我知道,当发生重定向时,出于安全原因,授权标头被删除。我也不确定外部 API 中的重定向。
我将HttpClientHandler
with添加AllowAutoRedirect = false
到我的HttpClient
var handler = new HttpClientHandler() …
Run Code Online (Sandbox Code Playgroud) c# httpclient unauthorized basic-authentication www-authenticate
我的要求是从某个网站下载一个abc.zip文件http://clientdownload.xyz.com/Documents/abc.zip
对于这个活动,我编写了一个python脚本,如下所示:
url_to_check = 'http://clientdownload.xyz.com/Documents/abc.zip'
username = "user"
password = "pwd"
p = urllib2.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, url_to_check, username, password)
handler = urllib2.HTTPBasicAuthHandler(p)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
zip_file = urllib2.urlopen(url_to_check).read()
file_name = 'somefile.zip'
meta = zip_file.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
with open(file_name, 'wb') as dwn_file:
dwn_file.write(zip_file.read())
Run Code Online (Sandbox Code Playgroud)
我在运行脚本时遇到以下错误:
文件"updateCheck.py",第68行,在check_update zip_file = urllib2.urlopen(url_to_check).read()文件"/usr/lib/python2.7/urllib2.py",第126行,在urlopen中返回_opener.open( url,data,timeout)文件"/usr/lib/python2.7/urllib2.py",第406行,打开响应= meth(req,response)文件"/usr/lib/python2.7/urllib2.py" ,第519行,在http_response'htt',请求,响应,代码,消息,hdrs)文件"/usr/lib/python2.7/urllib2.py",第444行,错误返回self._call_chain(*args)文件"/usr/lib/python2.7/urllib2.py",第378行,在_call_chain中结果= func(*args)文件"/usr/lib/python2.7/urllib2.py",第527行,在http_error_default中引发HTTPError (req.get_full_url(),code,msg,hdrs,fp)urllib2.HTTPError:HTTP错误401:未经授权
我已正确地给出了用户名和密码,但它会引发未经授权的错误.
当我尝试使用带有-http-user and --ask-password
选项的wget链接下载它时,我可以下载该文件.
同样使用相同的脚本,我可以正确地从其他服务器下载文件.
我运行此脚本以获取更多信息:
import urllib2, re, time, sys
theurl='http://clientdownload.xxx.com/Documents/Forms/AllItems.aspx'
req = urllib2.Request(theurl)
try: …
Run Code Online (Sandbox Code Playgroud) 我正在开发适用于 Windows Phone 8 的 Web 程序(我认为这并不重要)并使用Microsoft HTTP 客户端库
当用户尝试获取 URL 时,我需要知道当响应是 401 时需要什么类型的身份验证。如果是 NTLM、Basic、Digest 等,这很容易;所有支持的WinHTTP方案。您可以使用以下代码来获取 URL:
HttpResponseMessage response;
using (var httpClient = new HttpClient(httpHandler))
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, authenticationUri);
response = await httpClient.SendAsync(request, _cancelTokenSource.Token);
}
Run Code Online (Sandbox Code Playgroud)
检查需要哪种身份验证的最简单方法是使用该Headers.WwwAuthenticate.Contains
方法,例如检查是否NTLM
需要方案:
string scheme = "NTLM";
bool requiredScheme = response.Headers.WwwAuthenticate.Contains(new System.Net.Http.Headers.AuthenticationHeaderValue(scheme));
Run Code Online (Sandbox Code Playgroud)
如果scheme = "Bearer"
然后它总是给假。
以下是在尝试访问需要Azure Active Directory
身份验证的服务器时获得的。
使用Jason Chrome App 从网络服务器获取响应标头,我收到:
Pragma: no-cache
Date: Fri, …
Run Code Online (Sandbox Code Playgroud) 好吧,这是一个很长的问题,但我认为这是值得的。我们有什么:
一个示例虚拟 C# 控制台应用程序,启动自托管 ASP.Net WebAPI 服务(Microsoft.AspNet.WebApi.OwinSelfHost
NuGet 包):
class Program
{
static void Main(string[] args)
{
var url = "http://localhost:8080/";
Console.WriteLine($"Starting on {url}");
using (WebApp.Start<Startup>(url))
{
Console.WriteLine("Success! Press any key to stop...");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)OWIN启动类:
class Startup
{
public void Configuration(IAppBuilder app)
{
// enable Windows AND Anonymous authentication
var listener = app.Properties["System.Net.HttpListener"] as HttpListener;
listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous |
AuthenticationSchemes.IntegratedWindowsAuthentication;
// configure WebAPI
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
}
}
Run Code Online (Sandbox Code Playgroud)具有两个公共方法的示例 WebAPI 控制器:
[RoutePrefix("sample"), …
Run Code Online (Sandbox Code Playgroud)我有一个java应用程序,它基于kerberos WWW-Authenticate Negotiate通过浏览器对用户进行身份验证。所有基于 Chromium 的行为都相同 \xe2\x80\x94 发送应用程序不支持的 NTLM 票证,收到 401 Unauthorized 返回,提示输入登录名/密码,然后发送 kerberos 令牌。在 NTLM 尝试后收到 401 Unauthorized 时,Firefox 保持沉默(空白页面,没有提示)。
\n我可以强制 Firefox 显示提示,但只能使用 WWW-Authenticate Basic。
\n我已经在 Firefox 中尝试过:
\n谢谢。
\n我已经在Flask中开发了一个API,该API使用HttpBasicAuth对用户进行身份验证。API在提琴手中绝对正常,当我们通过错误的凭据时返回401,但是当我在登录页面上使用相同的凭据时,浏览器会弹出额外的弹出窗口。我真的不想看到这个额外的弹出窗口,它要求提供凭据(返回时浏览器的默认行为
401
与
WWW-Authenticate: Basic realm="Authentication Required"
)。
当在本地部署时,它工作正常,但在远程服务器上托管时,则无法工作。
我们如何实现401,让浏览器不显示弹出窗口以询问凭据。
www-authenticate ×12
c# ×3
kerberos ×3
ntlm ×3
http ×2
negotiate ×2
spnego ×2
.net ×1
ajax ×1
akka-http ×1
azure ×1
browser ×1
firefox ×1
flask ×1
httpclient ×1
httplistener ×1
logout ×1
oauth ×1
python ×1
python-2.7 ×1
scala ×1
unauthorized ×1
webserver ×1