Skip to main content

How to Encrypt the web.config in SharePoint


Granting Read Access to an RSA Encryption Key

Encrypt with Folder Location 
Step 1:
Go to the appropriate framework directory for the ASP.NET files:cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Step 2:
From here we can grant read access to an RSA encryption key by running this command:
aspnet_regiis -pef "system.web/sessionState" "<Web Application Directory Path>"
web application directory path sample (C:\inetpub\wwwroot\wss\VirtualDirectories\your web application folder)


With Application Pool
Step 1:
Go to the appropriate framework directory for the ASP.NET files:cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Step 2:
From here we can grant read access to an RSA encryption key by running this command:.\aspnet_regiis.exe -pa "NetFrameworkConfigurationKey" "IIS APPPOOL\MySite"



How to Identify the APP  Pool

"IIS APPPOOL\MySite" is the identity that my App Pool runs under. If you don't know what yours is, create an .aspx file in your website with the following content:
<%@ Page Language="C#" %> 
<% Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name); %>

Encrypting Sections of the Web.config File

At this point, we are ready to run the command that will actually encrypt the web.config. 
MAKE SURE THAT YOU HAVE A BACKUP OF ALL THE DATA STORED IN THE SECTION YOU ARE ABOUT THE ENCRYPT.
.\aspnet_regiis.exe -pe "connectionStrings" -app "/MySite"

If all went well, you should see
Microsoft (R) ASP.NET RegIIS version 4.0.30319.17929 Administration utility to install and uninstall ASP.NET on the local machine. 

Copyright (C) Microsoft Corporation. All rights reserved. Encrypting configuration section... Succeeded! 


but...
It Didn't Work!!!

If you setup your system like me, 
you may have encountered output containing a stupid error message like this one:Microsoft (R) ASP.NET RegIIS version 4.0.30319.17929 Administration utility to install and uninstall ASP.NET on the local machine. Copyright (C) Microsoft Corporation. All rights reserved. Encrypting configuration section... A configuration file cannot be created for the requested Configuration object. Failed!


b471code3 from the ASP.NET forums hit the nail on the head with the answer:"I'm assuming you already checked this out but what I'd pay special attention to is the -site option. 

If the app's web.config you are trying to encrypt is not under the DefaultWebSite or you have deleted and recreated the DefaultWebSite, the -site option will need to be specified. 

For example, when IIS is installed, a Web site named "Default Web Site" is created as site 1. In pages served from that site, the INSTANCE_META_PATH server variable returns "/LM/W3SVC/1". If you do not specify a -site option, site 1 is used."


But how do we get the site's INSTANCE_META_PATH? (Important)

Scott Forsynth tells you how to get the INSTANCE_META_PATH on his blog. Just make another .aspx file in your site with the following content:<%@ Page Language="C#" %> <% foreach (string var in Request.ServerVariables) { Response.Write(var + " " + Request[var] + "<br>"); } %>


That will dump all the server variables to the page, in which you will find something like this:INSTANCE_META_PATH /LM/W3SVC/3


The number on the end is the site ID (It also looks like the INSTANCE_ID variable has just the site ID, but I'm not 100% sure if that is reliable). Take that and incorporate it into the encryption command. This is what the correct command looks like:
.\aspnet_regiis.exe -pe "connectionStrings" -app "/" -site "3"


Note that I replace the application name with just a forward slash. If you do run an application inside your IIS site, you will need to include that. Personally, I don't normally do that, mainly to avoid issues with configuration inheritance.

And then you will have a super secret web.config section!

Comments

Popular posts from this blog

[Solved] SharePoint 2013 And Adobe Reader Problem : The URL you have provided could not be reached. Please verify that the URL is correct and that the network location is reachable

Dear All  When trying to open a PDF file from a mapped drive in SharePoint 2010.  You might see the following error message. The URL you have provided could not be reached. Please verify that the URL is correct and that the network location is reachable. 1. Open the registry. 2. Go to HKLM Local Software\SOFTWARE\Policies\Adobe\\\FeatureLockDown. 3. Create a key called cSharePoint. 4. Create a DWORD value called bDisableSharePointFeatures. 5. Set its value to 1. Regards Rashid Imran Bilgrami CEO Best visualization http://www.bestvisualization.com  

Content Query Webpart Template In SharePoint 2013 and add new column in it

Dear All, Here is the easiest way to make add custom column under your content query web part and then you can format it as you want. Step 1:  Open Sharepoint design Step 2:  Open site for sharepoint parent site collection Step 3:  Locate the following folder Style Library > XLS Style Sheets Step 4:  Open "ItemStyle.xsl"  file Add the custom style sheet as you want <xsl:template name="[Your Style Name]" match="Row[@Style='[Your Style Name]']" mode="itemstyle">                                                   <xsl:param name="CurPos" />                          ...

SQL SPLIT FUNCTION or String_split sql function alternative Very Information (Solved)

Dear All,  Today i created a form with the checkbox list and store the value in the sql with comma seprated way. It was strange for me that the split_string function is not supported in sql 2014, so i have created split function that will help to split the comma sepearted values   CREATE FUNCTION dbo.Split(@String varchar(8000), @Delimiter char(1))      returns @temptable TABLE (items varchar(8000))      as      begin      declare @idx int      declare @slice varchar(8000)      select @idx = 1          if len(@String)<1 or @String is null  return      while @idx!= 0      begin          set @idx = charindex(@Delimiter,@String)          if @idx!=0              set @slice = left(@String,@idx - 1)          else  ...