title: How to Prevent Direct access to View in Asp.net Core
body: <p>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.</p>
  
  <p>I have tried this option, but keep getting error as the <strong>UrlReferer</strong> is not available in Asp.net core. this code will not work in asp.net core, can someone help me please.</p>
  
  <p>How to access these values in ASp.net Core 2.x : </p>
  
  <pre><code>filterContext.HttpContext.Request.UrlReferrer
  filterContext.HttpContext.Request.Url.Host
  filterContext.HttpContext.Request.UrlReferrer.Host
  </code></pre>
  
  <p>Code I found on Stack-overflow - but it needs System.Web, which is not available in asp.net core.</p>
  
  <pre><code>
  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 = "" })); 
          }
      }
  }
  
  </code></pre>
  
  <p>Recommended way to use the custom Attribute</p>
  
  <pre><code>[NoDirectAccess]
  public ActionResult MyActionMethod()
  
  </code></pre>