ASP.Net MVC 3 przekierowanie nieautoryzowanego użytkownika nie do loginUrl
Mam projekt wykorzystujący ASP.Net MVC3 i korzystanie z członkostwa dla ról. używam autoryzacji w każdym kontrolerze. eg:
[Authorize(Roles = "Administrator")]
public ActionResult Index(string q, int i)
{
return View(model);
}
Jeśli ktoś nie ma roli administratora, domyślnie przekieruje na stronę logowania. jak to zmienić, aby przekierować na widoki / udostępnione/nieautoryzowane.cshtml ? a może jeśli ktoś nie ma roli administratora, wyświetli okno wiadomości (alert) ?
Z góry dzięki.6 answers
Wystarczy zmienić stronę, która ma być wyświetlana w sieci.config (sprawdź czy trasa istnieje)
<authentication mode="Forms">
<forms loginUrl="~/UnAuthorize" timeout="2880" />
</authentication>
Jeśli zamiast tego chcesz przekierować do określonej ścieżki dla każdej roli, możesz rozszerzyć AuthorizeAttribute o własną. Coś takiego (nie testowane, piszę to, aby dać ci pomysł)
public class CheckAuthorize : ActionFilterAttribute
{
public Roles[] Roles { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//Your code to get the user
var user = ((ControllerBase)filterContext.Controller).GetUser();
if (user != null)
{
foreach (Role role in Roles)
{
if (role == user.Role)
return;
}
}
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
if user.Role==Role.Administrator
{
redirectTargetDictionary.Add("action", "Unauthorized");
redirectTargetDictionary.Add("controller", "Home");
}
else
{
redirectTargetDictionary.Add("action", "Logon");
redirectTargetDictionary.Add("controller", "Home");
}
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-04 10:16:22
Rozwiązałem swój problem. ja tylko to robię:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
public class MyAuthorize : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
//you can change to any controller or html page.
filterContext.Result = new RedirectResult("/cpanel/roles/unauthorize");
}
}
I zastosuj moje uprawnienia do klasy lub pozwu:
[MyAuthorize]
public class AdminController :Controller
{
}
To wszystko.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-06 08:54:09
Cóż, możesz dziedziczyć z AuthorizeAttribute
i nadpisywać HandleUnauthorizedRequest
, które jest odpowiedzialne za przekierowanie nieautoryzowanych / nieautoryzowanych żądań. myślę, że to pytanie będzie dla ciebie pomocne.]}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:25:33
Moja własna wersja, oparta na NTEP vodka ' s:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if(IsUserAuthenticated(filterContext.HttpContext))
{
filterContext.Result = new RedirectResult("/Account/InvalidRole");
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
private bool IsUserAuthenticated(HttpContextBase context)
{
return context.User != null && context.User.Identity != null && context.User.Identity.IsAuthenticated;
}
}
W ten sposób otrzymuję standardowe przekierowanie do strony logowania dla nieautoryzowanych użytkowników, a niestandardowe przekierowanie dla użytkowników, którzy są uwierzytelnieni, ale nie mają odpowiedniej roli dla danej akcji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-17 16:45:00
Poniższy kod pomógł i tutaj jest odniesienie w stackoverflow ASP.NET MVC 4 custom Authorize attribute - jak przekierować nieautoryzowanych użytkowników na stronę błędu?
public class CustomAuthorize: AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new{ controller = "Error", action = "AccessDenied" }));
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:32:29
Używam tej metody i jest ona bardzo łatwa do wdrożenia.
Zmień domyślną trasę na stronę logowania w global.asax
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-28 22:28:16