I want to prevent an view from directly being accessed. I have a view that displays Email Confirmed, when a confirmation link in the users email is clicked. But, the problem is the Url of that view can be directly accessed, I just wan it to be accessed when the link in email is clicked.
I have tried this option, but keep getting error as the UrlReferer is not available in Asp.net core. this code will not work in asp.net core, can someone help me please.
How to access these values in ASp.net Core 2.x :
filterContext.HttpContext.Request.UrlReferrer
filterContext.HttpContext.Request.Url.Host
filterContext.HttpContext.Request.UrlReferrer.Host
Code I found on Stack-overflow - but it needs System.Web, which is not available in asp.net core.
public class NoDirectAccessAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.UrlReferrer == null ||
filterContext.HttpContext.Request.Url.Host != filterContext.HttpContext.Request.UrlReferrer.Host)
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new { controller = "Home", action = "Index", area = "" }));
}
}
}
Recommended way to use the custom Attribute
[NoDirectAccess]
public ActionResult MyActionMethod()
UrlRefereris easy to pass by. Also, if the user has confirmed the email, but access the view by pasting the URL directly in the browser twice, how do you determine whether that view should be displayed or not ? IMO, If the user has confirmed the email, simply store theEmailConfrimed=truesomewhere (Database/JWT/...). And now you could use a filter/policy to prevent the view from directly being accessed