1

In my JSON controller I want to check the access right from the User.

My code looks like this

public class MyController : SitecoreController
{

    public static bool CheckAccessRight()
    {
        if (Sitecore.Context.User.IsInRole("sitecore\\Sitecore Client Users"))
        {
            return true;
        }
        if (Sitecore.Context.User.IsAdministrator)
        {
            return true;
        }
        return false;
    }

    [System.Web.Http.HttpGet, System.Web.Http.HttpPost]
    public ActionResult DoSomethings(int page)
    {
      if (!CheckAccessRight())
      {
        return new JsonResult { Data = "access denied", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
      }
      return new JsonResult { Data = "my data", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }
}

The controller is registered in the initialize Pipeline. simiar to the documentation https://doc.sitecore.com/developers/91/sitecore-experience-management/en/use-mvc-routing.html. Works on older versions of Sitecore but in Sitecore 9.1 update-1 the Sitecore.Context.User gives not the logged in user but gives extranet\Anonymous

How can I get the actual user back?

Note: it fails in Sitecore 9.1 initial version and Sitecore 9.1 update-1. works on 9.0

4
  • 2
    You might find this useful: sitecore.stackexchange.com/q/17485/237
    – Gatogordo
    Commented Apr 9, 2019 at 13:30
  • Thank @Gatogordo it works when I add ?sc_site=shell to the Url, I had already try it with the siteSwitcher to shell but that fails. Commented Apr 9, 2019 at 13:35
  • Does this fail for GET and POST calls?
    – Chris Auer
    Commented Apr 9, 2019 at 16:01
  • Post and GET fail, but for me it resolved with adding shell to the query string Commented Apr 9, 2019 at 19:29

1 Answer 1

0

Add ?sc_site=shell to the url, in the shell context the Sitecore user will be correct.

See also IsAdministrator check no longer working on Sitecore 9.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.