我正在重写Devise的失败响应,以便我可以设置401状态代码.但是,当用户登录失败时,会将其重定向到"正在重定向"链接的页面.如果我:status => 401从重定向中删除它,它可以正常工作.
class CustomFailure < Devise::FailureApp
def redirect_url
new_user_session_url(:subdomain => 'secure')
end
def respond
if http_auth?
http_auth
else
store_location!
flash[:alert] = i18n_message unless flash[:notice]
redirect_to redirect_url, :status => 401
end
end
end
Run Code Online (Sandbox Code Playgroud)
或者,我想显示flash消息并保留在同一页面上,但添加以下代码行:
render :text => "unauthorized", :status => 401
Run Code Online (Sandbox Code Playgroud)
让红宝石抱怨:
undefined method `render' for #<CustomFailure:0x00000103367f28>
Run Code Online (Sandbox Code Playgroud)
这里发生了什么事?
redirect ruby-on-rails http-status-codes devise http-status-code-401
我正在使用Java访问HTTPS站点,该站点以XML格式返回显示.我在URL本身传递登录凭据.这是代码片段:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
requestURL = "https://Administrator:Password@localhost:8443/abcd";
try {
InputStream is = null;
URL url = new URL(requestURL);
InputStream xmlInputStream =new URL(requestURL).openConnection().getInputStream();
byte[] testByteArr = new byte[xmlInputStream.available()];
xmlInputStream.read(testByteArr);
System.out.println(new String(testByteArr));
Document doc = db.parse(xmlInputStream);
System.out.println("DOC="+doc);
} catch (MalformedURLException e) {
}
Run Code Online (Sandbox Code Playgroud)
我正在该程序中创建一个不验证签名/未签名证书的信任管理器.但是,在运行上述程序时,我收到错误服务器返回的HTTP响应代码:401 for URL:https:// Administrator:Password @ localhost:8443/abcd
我可以在浏览器上使用相同的URL,并正确显示xml.请告诉我如何在Java程序中完成这项工作.
我有一个包含WebForms和MVC页面的大型企业应用程序.它具有我不想更改的现有身份验证和授权设置.
WebForms身份验证在web.config中配置:
<authentication mode="Forms">
<forms blah... blah... blah />
</authentication>
<authorization>
<deny users="?" />
</authorization>
Run Code Online (Sandbox Code Playgroud)
到目前为止相当标准.我有一个REST服务,它是这个大型应用程序的一部分,我想使用HTTP身份验证代替这一项服务.
因此,当用户尝试从REST服务获取JSON数据时,它会返回HTTP 401状态和WWW-Authenticate标头.如果他们以正确形成的HTTP Authorization响应进行响应,则允许他们进入.
问题是,WebForms的覆盖这个处于较低的水平 - 如果返回401(未授权)它将覆盖与302(重定向到登录页面).这在浏览器中很好,但对REST服务没用.
我想关闭web.config中的身份验证设置,覆盖'rest'文件夹:
<location path="rest">
<system.web>
<authentication mode="None" />
<authorization><allow users="?" /></authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
该授权位工作正常,但认证线(<authentication mode="None" />)导致异常:
在应用程序级别之外使用注册为allowDefinition ='MachineToApplication'的部分是错误的.
我在应用程序级别配置它 - 它位于根web.config中 - 并且该错误适用于子目录中的web.configs.
如何覆盖身份验证,以便该站点的所有其余部分都使用WebForms身份验证,而这个目录不使用任何身份验证?
这类似于另一个问题:与ASP.NET MVC JSON请求401响应代码,但我不是在寻找相同的解决方案-我不想只是删除WebForms的认证和全球范围内添加新的自定义代码,还有远涉及很多风险和工作.我想只更改配置中的一个目录.
更新
我想设置一个Web应用程序,并希望所有WebForms页面和MVC视图都使用WebForms身份验证.我希望一个目录使用基本的HTTP身份验证.
请注意,我说的是身份验证,而不是授权.我希望REST调用带有HTTP头中的用户名和密码,我希望WebForm和MVC页面附带来自.Net的身份验证cookie - 在任何一种情况下,都会对我们的数据库进行授权.
我不想重写WebForms身份验证并滚动我自己的cookie - 这似乎是将HTTP授权的REST服务添加到应用程序的唯一方法,这似乎很荒谬.
我无法添加其他应用程序或虚拟目录 - 它必须作为一个应用程序.
有没有办法使用Windows命令在IIS 7中重建applicationHost.config文件?或者,如果您可以通过修改此文件来帮助我解决问题,那也没关系.将此文件与其他服务器进行比较时,我注意到在删除站点和几个应用程序池后缺少或不同的非常重要的部分.
我绝不是IIS 7的专家,但我已经使用它7年了.我有信心为新网站配置执行了所有正确的步骤,它仍然给我这个错误,"401.1 "You are not authorized to view this page. You do not have permission to view this directory or page using the credentials that you supplied."
C:\Windows\System32\inetsrv\config\applicationHost.config
Run Code Online (Sandbox Code Playgroud)
这发生在IIS APPPOOL {app pool user}不存在的机器上,所以我们从未使用过此用户,就像我通常在配置权限时那样.然而,我们上周在IIS_IUSRS(在此组中使用IUSR)中添加了适当的权限,并且该网站运行正常.我们正在使用Windows身份验证,并且所有其他身份验证方法都已关闭.没有虚拟目录.我们使用的是.NET 4.0,Classic和32位应用程序(在高级设置下).
我们必须通过在绑定的"C:\ Windows\System32\inetsrv"中执行以下命令来手动更新主机名,因为当VeriSign SSL证书添加到绑定时,IIS会使主机名变灰.
appcmd set site /site.name:"himc" /+bindings.[protocol='https',bindingInformation='*:443:subdomain.domain.com']
Run Code Online (Sandbox Code Playgroud)
缺少"坏"Web服务器上的部分:
<customMetadata>
<key path="LM/W3SVC/INFO">
<property id="4012" dataType="String" userType="1" attributes="Inherit" value="NCSA Common Log File Format,Microsoft IIS Log File Format,W3C Extended Log File Format,ODBC Logging" />
<property id="2120" dataType="MultiSZ" userType="1" attributes="None" value="400,0,,,0
" />
</key> …Run Code Online (Sandbox Code Playgroud) iis-7 windows-server-2008 applicationhost http-status-code-401
我们使用C#开发了一个WPF应用程序,并使用RestSharp与一个简单的Web服务进行通信,如下所示:
Client = new RestClient(serviceUri.AbsoluteUri);
Client.Authenticator = new NtlmAuthenticator(SvcUserName, SvcPassword.GetString());
Run Code Online (Sandbox Code Playgroud)
这一切都很有效,直到我们收到一些电话(大多数工作)的应用程序无法连接到服务.用fiddler直接调用服务方法.然后我们提取了一个小的.net控制台应用程序并尝试使用RestSharp进行服务调用并直接使用HttpWebRequest,然后再次使用401失败.现在我们启用了System.Net跟踪并注意到了一些事情.在第一个401,这是正常的,故障机器产生这个日志:
System.Net信息:0:[4480]连接#3741682 - 收到的标题{连接:保持活动内容长度:1293内容类型:text/html日期:星期一,10八月2015 12:37:49 GMT服务器:Microsoft -IIS/8.0 WWW-Authenticate:Negotiate,NTLM X-Powered-By:ASP.NET}.System.Net信息:0:[4480] ConnectStream#39451090 :: ConnectStream(缓冲1293字节.)System.Net信息:0:[4480]将HttpWebRequest#2383799与ConnectStream相关联#39451090 System.Net信息:0:[4480]将HttpWebRequest#2383799与HttpWebResponse相关联#19515494 System.Net信息:0:[4480]枚举安全包:System.Net信息:0:[4480]协商System.Net信息:0:[4480] NegoExtender System.Net信息:0 :[4480] Kerberos System.Net信息:0:[4480] NTLM系统.
System.Net信息:0:[4480] AcquireCredentialsHandle(package = NTLM,intent = Outbound,authdata = (string.empty)\ corp\svc_account)
System.Net信息:0:[4480] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY,context =(null),targetName = HTTP/mysvc.mycorp.com,inFlags = Delegate,MutualAuth,Connection)System.Net Information:0 :[4480] InitializeSecurityContext(In-Buffers count = 1,Out-Buffer length = 40,返回代码= ContinueNeeded).
工作机器产生这样的输出:
System.Net信息:0:[3432]连接#57733168 - Empfangene Statusleiste:Version = 1.1,StatusCode = 401,StatusDescription = Unauthorized.System.Net信息:0:[3432]连接#57733168 - 标题{Content-Type:text/html服务器:Microsoft-IIS/8.0 WWW-Authenticate:Negotiate,NTLM X-Powered-By:ASP.NET Date:Mon, 2015年8月10日15:15:11 GMT内容长度:1293} wurden empfangen.System.Net信息:0:[3432] ConnectStream#35016340 :: …
我有一个文件request.js,其中包含axios ajax请求的包装器.我从多个反应组件调用请求函数,当其中一个请求失败时,我想刷新令牌并再次重试所有失败的请求.我可以使用拦截器,但我不知道如何实现它.请帮忙.
request.js
var client = axios.create({
baseURL: 'http://192.168.1.3:3000',
headers: {
appID: 8,
version: "1.1.0",
empID: localStorage.getItem('empID'),
token: localStorage.getItem('accessToken')
}
});
const request = function(options) {
const onSuccess = function(response) {
console.debug('Request Successful!', response);
return response.data;
}
const onError = function(error) {
console.error('Request Failed:', error.config);
if (error.response) {
console.error('Status:', error.response.status);
console.error('Data:', error.response.data);
console.error('Headers:', error.response.headers);
} else {
console.error('Error Message:', error.message);
}
return Promise.reject(error.response || error.message);
}
return client(options)
.then(onSuccess)
.catch(onError);
options
}
export default request;
Run Code Online (Sandbox Code Playgroud) 我有一个在我的IIS6(XP专业版SP2)中运行的.NET 2.0网站(VB)和一个托管ASMX Web服务的.NET 3.5(在IIS的ASP.NET选项卡下配置为.NET2).
在Chrome中,我可以调用ASMX并成功调用Web方法.但是,在使用.NET 2.0网站调用代码中的Web方法时,我得到:
请求失败,HTTP状态为401:未授权.
我该如何解决这个问题?
我正在使用我的ASP.NET MVC 2项目.我创建了异常过滤器,用于捕获当用户无权查看某些操作时发生的未授权访问异常.
[CustomError(typeof(UnauthorizedAccessException), "Error", "UnauthorizedAccess")]
public class MyController : BaseController
{
}
Run Code Online (Sandbox Code Playgroud)
异常被抛出之后,我的过滤器将转移到配置的控制器/动作了下面的方法.
public ActionResult UnauthorizedAccess(ExceptionContext context)
{
Response.StatusCode = CustomHttpStatusCode.UnauthorizedUser;
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
最后,前ASP.NET应用结束该请求时,它将调用下面的方法,其位于Global.ascx用于改变自定义的HTTP状态代码到HTTP状态401(未授权访问).
public void Application_EndRequest(object sender, EventArgs e)
{
if (Response.StatusCode == CustomHttpStatusCode.UnauthorizedUser)
{
Response.StatusCode = 401;
}
}
Run Code Online (Sandbox Code Playgroud)
一切都在我的机器上工作正常(IIS 7.5).但它在我的部署网站上不起作用.它仍然返回纯文本"您没有权限查看该目录或网页." 而不是我的自定义错误页面.
PS.以下配置是本案例的当前web.config.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="On"></customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="502" subStatusCode="-1" />
<remove statusCode="501" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="412" subStatusCode="-1" />
<remove statusCode="406" subStatusCode="-1" />
<remove …Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用RoboSpice的应用程序.在请求侦听器onRequestFailure(SpiceException arg0)中是否有一种方法可以确定错误是由于发生了401 HTTP错误?
我有一个后端服务,当令牌到期时返回401错误,当发生这种情况时,我需要提示用户重新输入他们的凭据.
反正知道发生了401 HTTP错误吗?
以下是我的请求示例.
public class LookupRequest extends SpringAndroidSpiceRequest <Product> {
public String searchText;
public String searchMode;
public LookupRequest() {
super( Product.class );
}
@Override
public Product loadDataFromNetwork() throws Exception {
String url = String.format("%s/Lookup?s=%s&m=%s", Constants.BASE_URL, searchText, searchMode);
Ln.d("Calling URL: %s", url);
return getRestTemplate().getForObject(url, Product.class );
}
Run Code Online (Sandbox Code Playgroud) 如果请求用户未经过身份验证,我想发送一个401的响应代码,但我还想在请求是HTML请求时重定向.我一直发现Express 4不允许这样:
res.status(401).redirect('/login')
Run Code Online (Sandbox Code Playgroud)
有谁知道处理这个的方法?这可能不是Express的限制,因为我要求基本上传递两个标题,但我不明白为什么会出现这种情况.我应该能够传递"未经过身份验证"的响应并将用户一次性重定向.