8

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.

EvilDr
  • 8,943
  • 14
  • 73
  • 133

3 Answers3

25

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)
                            }));
hkutluay
  • 6,794
  • 2
  • 33
  • 53
EvilDr
  • 8,943
  • 14
  • 73
  • 133
1

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.

D.L.MAN
  • 990
  • 12
  • 18
0

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!

Leo
  • 1