HttpContext.Current.Request.IsAuthenticated和HttpContext.Current.User.Identity.IsAuthenticated有什么区别?
你会在哪种情况下使用哪一个?
我在ASP.NET MVC 5应用程序中使用自定义authorize属性,如下所示:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext context)
{
if (context.HttpContext.Request.IsAuthenticated)
{
context.Result = new System.Web.Mvc.HttpStatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
}
else
{
base.HandleUnauthorizedRequest(context);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在system.web我的web.config部分,我提到了错误路径,如:
<system.web>
<customErrors mode="On" defaultRedirect="/Error/Error">
<error statusCode="403" redirect="/Error/NoPermissions"/>
</customErrors>
</system.web>
Run Code Online (Sandbox Code Playgroud)
但我从未被重定向到我的自定义错误页面/Error/NoPermissions.相反,浏览器显示"HTTP错误403.0 - 禁止"的常规错误页面.
我有一个使用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
我的角度应用程序正在使用文章系列中所述的持有人令牌http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net - 身份/.我已经按照分叉示例在访问令牌到期时无缝刷新令牌(通过401 http代码).
我的问题是如何根据确定的角色确定持有人令牌是否已过期或仅仅未经授权?
例如,我的web api方法具有[Authorize(Roles ="Admin")]属性.当我打电话给那个时,我收到了我的401错误,这是预期的.但是,当我的访问令牌过期并且我进行另一个web api方法调用时,它也会返回401错误.继承我的拦截器中的responseError处理程序:
responseError: function (rejection) {
var deferred = q.defer();
if (rejection.status === 401) {
var authService = $injector.get('authService');
authService.refreshToken().then(function (response) {
_retryHttpRequest(rejection.config, deferred);
}, function () {
authService.logOut();
$location.path('/dashboard');
deferred.reject(rejection);
});
} else {
deferred.reject(rejection);
}
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
我正在玩不同的东西,但基本上,我想刷新我的令牌并在访问令牌到期时重新发送我的请求; 但是,如果由于指定的角色确实是拒绝请求,我不想刷新我的令牌.
有什么想法吗?
我在使用.Net MVC 5 app配置ADFS时遇到问题.
我已经在VS 2015中配置我的项目以使用声明并且它工作正常,但我有一个问题.
我可以登录,使用ADFS,我可以检查用户角色等.当我尝试使用时出现问题
[Authorize(Roles="somenonExistingRole")]
Run Code Online (Sandbox Code Playgroud)
尽管我已经通过身份验证,但是当重新进行身份验证时,我被重定向到ADFS页面,并且我被重定向到我的页面,其中发生了循环.页面将我发送到ADFS门户,ADFS将我重定向到门户网站,并在几次尝试后从ADFS(到许多请求)收到错误
我是否必须自己实施角色提供程序?或者我需要配置额外的东西.也许我可以限制尝试次数?当我的角色已经完成时,为什么我会被重定向到ADFS?
在代码中没有多少显示实际,按要求:我测试的控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[Authorize]
public ActionResult About()
{
var u = HttpContext.User;
if (u.IsInRole("/"))
{
ViewBag.Message = "User is in role.";
}
else
{
ViewBag.Message = "User is NOT in role.";
}
return View();
}
[Authorize(Roles = "/nonexistingRole")]
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
和配置身份验证部分
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new …Run Code Online (Sandbox Code Playgroud) 我在一个位在ASP.NET MVC与实施角色绑定5.我试图登录为不具有访问我试图达到应用领域所需的角色的用户.我在这种情况下的期望是,我再次被重定向到登录页面,直到我输入一组有访问权限的凭据或我导航到应用程序的另一个区域.
实际发生的是应用程序似乎进入登录重定向循环,调试通过显示多次调用Login操作.
这是登录操作:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
Run Code Online (Sandbox Code Playgroud)
这会导致IIS生成错误:
HTTP Error 404.15 - Not Found
The request filtering module is configured to deny a request where the query string is too long.
Run Code Online (Sandbox Code Playgroud)
查询字符串如下所示:
http://localhost/MyApplication/Account/Login?ReturnUrl=%2FMyApplication%2FAccount%2FLogin%3FReturnUrl%3D%252FMyApplication%252FAccount%252FLogin%253FReturnUrl%253D%25252FMyApplication%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FMyApplication%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D%252525252FMyApplication%252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FMyApplication%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FMyApplication%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525252FMyApplication%252525252525252FAccount%252525252525252FLogin%252525252525253FReturnUrl%252525252525253D%25252525252525252FMyApplication%25252525252525252FAccount%25252525252525252FLogin%25252525252525253FReturnUrl%25252525252525253D%2525252525252525252FMyApplication%2525252525252525252FAccount%2525252525252525252FLogin%2525252525252525253FReturnUrl%2525252525252525253D%252525252525252525252FMyApplication%252525252525252525252FAccount%252525252525252525252FLogin%252525252525252525253FReturnUrl%252525252525252525253D%25252525252525252525252FMyApplication%25252525252525252525252FAccount%25252525252525252525252FLogin%25252525252525252525253FReturnUrl%25252525252525252525253D%2525252525252525252525252FMyApplication%2525252525252525252525252FAccount%2525252525252525252525252FLogin%2525252525252525252525253FReturnUrl%2525252525252525252525253D%252525252525252525252525252FMyApplication%252525252525252525252525252FAccount%252525252525252525252525252FLogin%252525252525252525252525253FReturnUrl%252525252525252525252525253D%25252525252525252525252525252FMyApplication%25252525252525252525252525252FAccount%25252525252525252525252525252FLogin%25252525252525252525252525253FReturnUrl%25252525252525252525252525253D%2525252525252525252525252525252FMyApplication%2525252525252525252525252525252FAccount%2525252525252525252525252525252FLogin%2525252525252525252525252525253FReturnUrl%2525252525252525252525252525253D%252525252525252525252525252525252FMyApplication%252525252525252525252525252525252F
Run Code Online (Sandbox Code Playgroud)
我从一个有效的解决方案(尽管没有基于角色的授权)到我当前破碎的情况所做的唯一改变是在成功登录时重定向到的控制器上面添加了以下内容:
[Authorize(Roles = "Staff")]
Run Code Online (Sandbox Code Playgroud)
正如我之前所说,我登录的用户不是这个角色,但我希望在没有循环的情况下,理智,单一重定向到Login.
编辑:请求bu @dima,通过过滤器应用授权的详细信息......我有以下内容:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AuthorizeAttribute());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我已经使用和不使用此行测试了应用程序,并且重定向循环继续有增无减.
asp.net-mvc authorize-attribute asp.net-roles asp.net-identity
我正在使用Windows身份验证创建Intranet站点.
也许我不是最好的方式,但是我试图通过调用一个包含Authorize action过滤器的控制器方法来加载部分视图,这样只有经过授权的人才能看到页面的那一部分.比如说,我想在页面上加载管理员工具,但前提是登录的个人是管理员.
所以在index.cshtml页面上我可能有类似的东西:
@Html.Action("LoadAdminTools","ControllerName")
Run Code Online (Sandbox Code Playgroud)
Controller将包含以下代码:
[Authorize(Roles="Admins")]
public ActionResult LoadAdminTools()
{
return PartialView("_AdminToolsPartialView");
}
Run Code Online (Sandbox Code Playgroud)
然后,包含管理控件(或其他)的部分视图将呈现给页面 - 仅当登录用户是Admins角色的一部分时.
我遇到的"问题"是,如果登录的人无权加载部分视图,浏览器会弹出登录对话框,询问用户的凭据.关闭对话框而不输入任何凭据会导致预期的结果 - 部分视图不会在页面的其余部分加载时加载.很酷,但很烦人.输入错误的凭据,您会收到401错误 - 也正如预期的那样.
如果有帮助:在IIS中,禁用匿名身份验证,启用 Windows身份验证.在"安全设置 - 本地Intranet区域"下的"Internet选项"中选择"使用当前用户名和密码自动登录".
我的问题是:有没有办法在没有浏览器要求用户登录的情况下使用[授权]操作过滤器加载部分视图(或者真的做任何事情)?只需要获取当前登录的凭据,检查它们是否符合操作过滤器,如果有,则加载部分视图,如果没有,则不要.如果没有,是否有更好的方式来实现我想在这里完成的工作?
UPDATE
美丽.我读了你发布的问题的解决方案,Mystere Man,在Controller的文件夹中创建了一个名为IntranetAuthorizeAttribute.cs的新类,抛出了代码:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class IntranetAuthorizeAttribute : 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)
用我的新IntranetAuthorize过滤器替换了Authorize过滤器:
[IntranetAuthorize(Roles="Admins")]
public ActionResult LoadAdminTools()
{ …Run Code Online (Sandbox Code Playgroud) 我Microsoft.Owin.Security在我的应用程序中使用(.NET 4.5上的ASP.NET MVC v 5.2.0).但只是安全部分OWIN没有别的.当用户想要访问本地受保护的URL时,请求将被重定向到登录页面.但是当我在服务器上发布应用程序时,我得到这个窗口而不是重定向:

我的登录和注销方法是:
public void LogIn(long userId, string username, string email, bool persistent) {
var claims = new List<Claim>{
new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture)),
new Claim(ClaimTypes.Name, username),
new Claim(ClaimTypes.Email, email),
new Claim(ClaimTypes.IsPersistent, persistent.ToString())
};
var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = HttpContext.Current.Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
authenticationManager.SignIn(new AuthenticationProperties {
IsPersistent = persistent
}, id);
}
public void LogOut() {
var ctx = HttpContext.Current.Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignOut();
}
Run Code Online (Sandbox Code Playgroud)
这是我的创业公司:
public partial …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×5
owin ×2
.net ×1
adfs ×1
angularjs ×1
asp.net ×1
bearer-token ×1
claims ×1
iis ×1
redirect ×1
roles ×1
unauthorized ×1