我有许多ajax动作的应用程序(使用JQuery.ajax实现),它返回JSON ot html.其中一些应该只有授权用户才能访问,我用[Authorize]
属性修饰它们.对于非ajax操作,如果用户未被授权 - 系统将其重定向到登录页面(在web.config中配置).
但是这不适用于ajax操作,因为如果用户被授权 - 他加载页面,在cookie过期后他没有被授权,而不是html块,它应该替换旧的,他在块内得到我的登录页面.
我知道我可以手动解决这个问题,例如删除[Authorize]属性,如果用户没有授权,则返回特殊的json/empty html.但我不喜欢这个解决方案,因为我需要重写我的所有动作和ajax函数.我想要全局解决方案,允许我不要重写我的操作(可能是自定义授权属性,或某些http未经授权的结果自定义处理).
我想返回状态代码,如果请求是ajax,并且重定向到登录页面,如果请求不是ajax.
asp.net-mvc jquery unauthorized http-status-code-401 asp.net-mvc-3
我有一个使用ASP.Net MVC3并使用角色成员资格的项目.我在每个控制器中使用授权.例如:
[Authorize(Roles = "Administrator")]
public ActionResult Index(string q, int i)
{
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
如果某人没有管理员角色,则默认情况下会重定向到登录页面.如何更改它,所以它将重定向到Views/Shared/UnAuthorize.cshtml?或者如果有人没有管理员的角色,它会显示消息框(警告)?
提前致谢.
redirect asp.net-membership roles unauthorized asp.net-mvc-3
我是AngularJS的新手,现在花了3天时间找到处理401状态的方法.我尝试过使用$ http的拦截器,使用$ resource ...但没有任何工作.我的应用程序在同一台服务器上调用JSONP调用.当发生错误时,它会被错误回调函数捕获.但状态始终为0且响应未定义.
首先,我尝试了这个拦截器
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push(['$q', function($q) {
return function(promise) {
return promise.then(function(response) {
console.log('success in interceptor');
return response;
}, function(response) {
console.log('error in interceptor');
console.log(response);
if (response.status === 401) {
response.data = {
status: false,
description: 'Authentication required!'
};
return response;
}
return $q.reject(response);
});
}
}]);
}]);
Run Code Online (Sandbox Code Playgroud)
其次,还尝试使用$ resource的控制器
$scope.fetchData = function(fromDate, toDate){
Cancel.get({from: fromDate, to: toDate, perPage: 99999},
function(data){
$scope.cancels = $scope.filteredCancels = data.data;
$scope.search();
},
function(response) {
$scope.errorMessage = '<h4>Error : '+response.status+'</h4>';
window.location …
Run Code Online (Sandbox Code Playgroud) 我有一个MVC网站,其中访问基于各种角色.一旦用户登录系统,他们就可以看到导航到他们被授权的页面.但是,某些用户可能仍尝试使用直接URL访问页面.如果是这样,系统会自动将它们重定向到登录页面.而不是登录页面我想将它们重定向到另一个视图(未授权).
Web.Config具有以下条目:
<customErrors mode="On">
<error statusCode="401" redirect="~/Home/Unauthorized" />
<error statusCode="404" redirect="~/Home/PageNotFound" />
</customErrors>
<authentication mode="Forms">
<forms name="Development" loginUrl="~/Account/Login" cookieless="UseCookies" timeout="120"></forms>
</authentication>
Run Code Online (Sandbox Code Playgroud)
我也在Global.asax.cs中注册了这些路由.
routes.MapRoute(
name: "Unauthorized",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Unauthorized", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "PageNotFound",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "PageNotFound", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
它够了吗?
我重写该类以执行自定义授权
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(403);
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在在web.config中我配置了403错误页面
<customErrors defaultRedirect="/Shared/Error" mode="On">
<error statusCode="403" redirect="/Shared/UnAuthorize" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
但是浏览器仍然显示403的默认错误页面,我在这里缺少任何想法
将我的用户名和密码复制/粘贴到Docker Hub网站可以正常工作.
密码很长,但不包含破壳符号.
将这些相同的凭据复制/粘贴到命令行中会docker login
导致incorrect username or password
错误.我尝试以交互方式(复制/粘贴和键入)和命令行参数传递凭据,结果相同:
# INTERACTIVE
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: my@email.com
Password: <REDACTED>
Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
# COMMAND LINE
$ docker login -u my@email.com -p <REDACTED>
Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用WGET批处理文件从这里下载公共FCC文件
http://wireless.fcc.gov/uls/data/complete/l_micro.zip
当我最初使用参数运行批处理文件时
wget --server-response -owget.log http://wireless.fcc.gov/uls/data/complete/l_micro.zip
Run Code Online (Sandbox Code Playgroud)
它因HTTP 401未授权错误而失败.我可以在这一点上重试并且它一直在失败.但是我注意到,如果我打开IE,开始下载并在提示保存时取消,我可以重新运行批处理文件,它可以完美执行!
这是我日志中的详细服务器响应
--2012-02-06 14:32:24-- http://wireless.fcc.gov/uls/data/complete/l_micro.zip
Resolving wireless.fcc.gov (wireless.fcc.gov)... 192.104.54.158
Connecting to wireless.fcc.gov (wireless.fcc.gov)|192.104.54.158|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 302 Found
Location: REMOVED - appears to have my IP
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Connection: close
Content-Length: 513
Location: REMOVED [following]
--2012-02-06 14:32:24-- REMOVED
Resolving REMOVED... 192.168.2.11
Connecting to REMOVED|192.168.2.11|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
WWW-Authenticate: NTLM
WWW-Authenticate: BASIC realm="AD_BCAAA"
Content-Type: …
Run Code Online (Sandbox Code Playgroud) 我已经在stackoverflow中阅读了很多主题来解决我的问题,但没有一个是有用的.
当我尝试使用许多不同的配置登录Manager App([http:// localhost:8080/manager/html] [1])时,但在尝试使用权限凭据登录后,我总是获得401 Unauthorized.我重启了tomcat服务器几次.
这是我在conf/tomcat-users.xml中的最后一次配置
<?xml version="1.0" encoding="UTF-8"?>
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status, admin-gui, admin-script"/>
</tomcat-users>
Run Code Online (Sandbox Code Playgroud)
这是与server.xml中的tomcat-users相关的部分
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
Run Code Online (Sandbox Code Playgroud)
这是我的配置:Apache Tomcat:版本7.0.40 64位在Chrome 26上测试
如果我忘记了其他有用的东西,请告诉我.提前致谢
如何使用C#安装字体?
我尝试使用复制字体,File.Copy()
但由于访问权限限制,我不被允许(UnauthorizedException
).
我该怎么办?
当我从Controller返回NotAuthorized IActionResult时,我一直在尝试阻止重定向,但无论我的尝试如何,NotAuthorized都会转换为Redirect.
我已经尝试了这里提到的(同样的问题,使用旧的beta框架,我使用1.0.0-rc1-final).我没有Notifications命名空间(已在rc1-final中删除).
这是我的登录控制器:
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return Ok(model);
}
if (result.IsLockedOut)
{
return new HttpStatusCodeResult((int)HttpStatusCode.Forbidden);
}
else
{
return HttpUnauthorized();
}
}
return HttpUnauthorized();
}
Run Code Online (Sandbox Code Playgroud)
在Startup.cs中,我尝试了以下变体:
services.Configure<CookieAuthenticationOptions>(o =>
{
o.LoginPath = PathString.Empty;
o.ReturnUrlParameter = PathString.Empty;
o.AutomaticChallenge = false;
});
Run Code Online (Sandbox Code Playgroud)
每次登录失败时(请忽略密码在Ok上返回)并且应该导致空的401页面,我将重定向到/ Account/Login.这里的诀窍是什么?
redirect identity unauthorized asp.net-core-mvc asp.net-core