问题
I have a custom authorize attribute:
using System;
using System.Web.Mvc;
using System.Web.Routing;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Login", action = "Login" }));
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
...that I use to decorate certain controllers:
[MyAuthorizeAttribute(Roles = "Superman, Batman, Spiderman")]
public class SuperHeroController : Controller
{
// ....
}
Can anyone please explain how to amend the authorize code so that if authorization fails, the Login URL includes a ReturnUrl
(URL of the current controller/method)?
This is basically trying to imitate the web forms ReturnUrl logic but in a smart manner whereby I don't have to manually use a string for the URL.
回答1:
Finally figured it out, although somebody might be able to suggest a better way...
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(
new
{
controller = "Login",
action = "Login",
returnUrl = filterContext.HttpContext.Request.Url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped)
}));
回答2:
there are many way to implement this.
you should use filterContext.HttpContext.Request.Url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped)
for getting returnUrl.
first way:
var returnUrl = filterContext.HttpContext.Request.Url?.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) ?? "";
if (!string.IsNullOrWhiteSpace(returnUrl))
{
returnUrl = "/" + returnUrl;
}
filterContext.Result = new RedirectResult($"~/Login/Login{returnUrl}");
second way:
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(
new
{
controller = "Login",
action = "Login",
area = "",
returnUrl = filterContext.HttpContext.Request.Url?.GetComponents(UriComponents.PathAndQuery,
UriFormat.SafeUnescaped)
}));
just , dont forget set Area.
回答3:
This was just what I needed for the same problem although looked slightly different:
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Account" },
{ "action", "Login" },
{ "returnUrl", filterContext.HttpContext.Request.Url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
});
Thanks!
来源:https://stackoverflow.com/questions/29800361/generate-a-return-url-with-a-custom-authorizeattribute