Verifying anonymous access to content with PowerShell

In this recipe, we will use PowerShell to ensure that anonymous users can access the home page of our SharePoint site but cannot access the Site contents page.

How to do it…

Follow these steps to verify the anonymous access to content with PowerShell:

  1. Create a new WebClient object. We are using the WebClient object to make simple, unauthenticated web requests against our SharePoint site.
    $client = New-Object System.Net.WebClient
    
  2. Use the DownloadString method to make a request for the home page of our site as follows:
    $client.DownloadString("http://sharepoint")
    

    If we receive the HTML content for our page, our request was successful. However, if we receive an exception with a 401 or 403 HTTP response, anonymous access is most likely not available for that page.

  3. Use the DownloadString method to make a request for the Site contents page on our site:
    $client.DownloadString("http://sharepoint/_layouts/viewlsts.aspx")
    

    If the page is correctly blocked for anonymous users, an exception should be thrown with a 401 or 403 HTTP response. If we receive the HTML content for the page, it indicates that the page request was successful and our page is not being blocked for anonymous users.

How it works…

Using the DownloadString method of the WebClient object, we are making simple HTTP requests in the same manner that a web browser would request the content. When the request is successful, it returns the content of the page as a plain text string object. When the request fails, an exception is thrown with the HTTP response code returned by the web server.

There's more…

Using the WebClient object to make HTTP requests against our SharePoint site may also be accomplished with code using the server-side object model. Follow these steps to verify anonymous access to content with code using the server-side object model:

  1. Create a new WebClient object as follows:
    var client = new WebClient();
  2. Use the DownloadString method to make a request for the home page of our site.
    var homePageContent = client.DownloadString("http://sharepoint");

    If we receive the HTML content for our page, our request was successful. However, if we receive an exception with a 401 or 403 HTTP response, anonymous access is most likely not available for that page.

  3. Use the following DownloadString method to make a request for the Site contents page on our site:
    var viewAllContent = client.DownloadString("http://sharepoint/_layouts/viewlsts.aspx");

    If the page is correctly blocked for anonymous users, an exception should be thrown with a 401 or 403 HTTP response. If we receive the HTML content for the page, it indicates that the page request was successful and our page is not being blocked for anonymous users.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.85.181