Monday, June 14, 2004 5:08 PM Jan Tielens

SharePoint, Code Access Security and the SmartPart

Security is hot these days, and that’s a good thing. But from the developer’s point of view, security can be a bitch sometimes. Code Access Security (CAS for short) is a wonderful and very useful concept, but developers need to know keep CAS in mind if they want their code to run. If you want to learn more about CAS in general, I definitely recommend Maxim Karpov’s article as a starter.

When you’re building SharePoint webparts (and when you’re doing ASP.NET in general), CAS really comes into play because in most scenario’s your code won’t have Full Trust. This means that it can’t do just anything, everything needs to be explicitly granted. For more information about CAS in combination with SharePoint, check out this article on MSDN.

When you’re using the SmartPart for SharePoint (also see my previous post) you’re in a special scenario with three involved parties: SharePoint, the SmartPart and your code. Let’s say you want to create a somewhat advanced user control (to show in the SmartPart) that connects to a web service (or a database), you’ll run into some problems. If you’ve already tried to do that when using the SmartPart, you’ll find the following exception in the Event Log:
Request for the permission of type System.Net.WebPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.

Why do we get that exception? By default, all the code that’s executed in the \BIN folder of the SharePoint site, uses a reduced set of permissions, specified in the web.config file (e.g. WSS_Minimal or WSS_Medium). The permission to connect to a web service (WebPermission) or to connect to a database, is not granted by default. That’s why the code won’t run. To solve this issue, one would think “let’s add that permission for the webpart”. But remember we’re using the SmartPart webpart, so adding permissions won’t help us because the user control code still has does not get the needed permissions. An easy, but drastic solution is to deploy the code to the Global Assembly Cache (GAC). Every assembly in the GAC, runs in Full Trust. Installing the SmartPart in the GAC is easy, during the installation the question is asked whether you want the assembly to be installed in the GAC or not. Deploying the user control’s assembly to the GAC is also quite easy (if your assembly has a strong name of course): drag-and-drop the .dll file to c:\windows\assembly. But there’s only one issue to solve: you’ll notice that once you’ve deployed the user control’s assembly to the GAC (and deleted it from the \BIN folder of course), you’ll get an exception (unable to load). So it seems that the SmartPart does not look in the GAC to load the user control’s assembly. To fix this (last!) problem, you need make some modifications to the web.config file of the SharePoint site: in the “compilation” section you need to add the user control’s assembly:
<compilation batch="false" debug="false">
  <assemblies>
    <add assembly="DropDownNavigationVB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3948f234bbbabe18" />
    </assemblies>
</compilation>

All these steps may be a little bit overwhelming, so let’s summarize and create an example. The example will connect to a web service and fetch some weather information (I’ve used this web service from CapeScience).

  • Step 1: install the SmartPart and make sure you deploy it to the GAC.
  • Step 2: create a new ASP.NET web application that will contain our user control, name it for example WeatherInfo. Add a web reference to the GlobalWeather wsdl and name it WeatherServices for example. Add a new Web User Control to the project and name it WeatherInfo and add a Label control to it. Switch to code view, and type following code in the Page_Load method:
    private void Page_Load(object sender, System.EventArgs e)
    {
        WeatherServices.GlobalWeather gw = new WeatherServices.GlobalWeather();
        Label1.Text = "Temperature in Brussels: " + 
               gw.getWeatherReport("EBBR").temperature.ambient.ToString();
    }
    Because we will have to deploy the user control’s assembly to the GAC, we need to give it a strong name. So generate a public/private key pair, and specify it in the AssemblyKeyFileAttribute in the AssemblyInfo file. (more detailed information about this process can be found here) Additionally you can change the version number specified in the AssemblyInfo file to a fixed version number (e.g. 1.0.0.0), otherwise every build a new version number will be generated. Finally, you can build the project.
  • Step 3: deploy the user control’s assembly. To deploy the WeatherInfo.dll file to the GAC, you can just drag-and-drop it to the c:\windows\assembly directory, or you can use the GACUTIL utility. The WeahterInfo.ascx file needs to be copied for example to the \UserControls folder of the SharePoint site (this is not a default folder, so you need to create it yourself).
  • Step 4: Alter the web.config. As mentioned before you need to alter the web.config of the SharePoint site, so the user control’s assembly can be located in the GAC. To do this, find the compilation section, that looks like this:
    <compilation batch="false" debug="false" />
    and change it to:
    <compilation batch="false" debug="false">
      <assemblies>
        <add assembly="WeatherInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6185e98411448c6a" />
        </assemblies>
    </compilation>
    I use the Reflector tool to read the public key of my assembly (you can copy-and-paste the full assembly name, so you won’t make any typos).
  • Step 5: load the user control in SharePoint. Finally you can add a SmartPart webpart to a SharePoint site, that will load the WeatherInfo control. Set the User Control property to ~\UserControls\WeatherInfo.ascx and click the OK button. If everything went well, you now should be able to see the temperature in Brussels!

To conclude, I’ll repeat one of the first sentences of this post: security can be a bitch sometimes. :-) But we must think about CAS as a good thing. Even the extra steps considered, that need to be taken to get the WeatherInfo webpart running, I still think creating SharePoint webparts with the help of ASP.NET User Controls is a productive approach. But at the same time I’m wondering about how the GAC deployment step can be avoided. Anyone has an idea?

You can download the WeatherInfo solution from the GotDotNet Workspace. In the next post I’ll dive into how you can easily test-drive webparts when using the SmartPart webpart.

Filed under:

Comments

# re: SharePoint, Code Access Security and the SmartPart

Monday, June 14, 2004 1:28 PM by Maxim V. Karpov

Jan,
Of course, it is possible to run User Controls in Bin direcotry without registering anything inside GAC. Just keep in mind that if you want functionality avalable throught different vservers then GAC is good solution!

I also noticed that you are using Wppackager deployment took way to go. One bug you have in the wppackager.xml file is you pasted short token value not the blob use secutil to retrieve the full strong name blob. I will make a write up on this isse :)

Stay tune, Maxim

[www.ipattern.com do you?]

# re: SharePoint, Code Access Security and the SmartPart

Monday, June 14, 2004 10:06 PM by Maxim V. Karpov

Jan,
Simple solution for the GAC problem will be. 1. Compile everything with the same *.snk file.
2. Then use C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\SecUtil.exe -hex -s [path_dll]
3. Add following Code group
<CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust">
<IMembershipCondition version="1" class="StrongNameMembershipCondition" PublicKeyBlob="0x0024000004800000940000000602000000240000525341310004000001000100BB20A2F20001218C2100806C4A656705F4220FE7636574B9B8128E0AA44BE734F379B88130A29020D658ED8CFA1B0A0C93094A22144C54476A507FBB5D812C94DD412AB260EED31D99857A53E20200AC9020007D852540DB43C54D5269507DD45181B1B5E6766C9E2FB3DD0C931C2D1B22ADFDCEC8FDB02E423E3D8BBD59C4B9" />
</CodeGroup>
Right after the following in the WSS_Minimal
<CodeGroup class="FirstMatchCodeGroup" version="1" PermissionSetName="Nothing">
<IMembershipCondition class="AllMembershipCondition" version="1" />

4. IISRest.exe and you ready to go

I hope this hepls. Maxim

BTW. I am working on the second article on CAS which will cover this in some details
[www.ipattern.com do you?]

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, June 15, 2004 3:28 AM by Jose

Hi Jan,

my .NET skills aren't any good so pardon if the question is somehow trivial. It seems I cannot deploy the DropDownNavigation.dll (or the treeview for that matter) in my \windows\assembly directory. I'm getting an error that states the dll has not a strong (secure?) name (more or less, take into account that the error is in spanish). So I can't go any further. Any help with this?

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, June 15, 2004 3:37 AM by Jan

If you want to deploy the DropDownNavigation.dll to the GAC, it will need a strong name. It comes down to generating a public/private key pair with the SN utility, and adding that key to the AssemblyInfo. The complete process is described in this article: http://www.dotnetspider.com/Technology/KBPages/406.aspx

Remember that you don't need to deploy the DropDownNavigation to the GAC, unless of course you've made some adjustements that require GAC deployment.

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, June 15, 2004 5:33 AM by Peter

I just follow the instructions to install the SmartPart. I want to try the dropdown navigation example. However, after placing the web part and fill in the path, it returns this error:
"Error: unable to load ~\UserControls\Dropdownnavigation.ascx
Details: Request for the permission of type Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c failed."
Please please help.
P.S. I am not a developer. So, please speak it slowly. Thanks a lot.

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, June 15, 2004 9:30 AM by Thom Caffrey

I also recieved the following error when I attempted to deply the DropDownNavigation user control.

Error: unable to load ~\UserControls\DropDownNavigation.ascx
Details: Request for the permission of type Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c failed.

Please Help...

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, June 15, 2004 2:22 PM by Maxim V. Karpov

Jan,
I not sure if you saw this article or not but here is a great explaintion of all ther options you have to calling Web Services
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_sp2003_ta/html/sharepoint_wsscodeaccesssecurity.asp

Maxim
[www.ipattern.com do you?]

# re: SharePoint, Code Access Security and the SmartPart

Wednesday, June 16, 2004 3:27 AM by Jose

Well, that error previously stated:

Error: unable to load ~\UserControls\DropDownNavigation.ascx
Details: Error de solicitud de permiso de tipo Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c.

is the same i was getting. I pretended to solve it by deploying to the GAC (no luck) but it seems that's not the cause of the problem at all.

Any idea Jan?

Thx for the good work

# Code Access Security: Partially Trusted Code (Article)

Thursday, June 17, 2004 7:24 PM by TrackBack

# re: SharePoint, Code Access Security and the SmartPart

Wednesday, June 23, 2004 8:11 AM by Poonam Thawani

Hi I have tried Smart Part coupe of times.But every time I get the following error message.Kindly help.

The "UserControlWebpart" Web Part appears to be causing a problem.

Web Parts Maintenance Page: If you have permission, you can use this page to temporarily disable Web Parts or remove personal settings. For more information, contact your site administrator.

# re: SharePoint, Code Access Security and the SmartPart

Wednesday, June 23, 2004 4:51 PM by Mike

I was feeling left out.......

Error: unable to load ~\UserControls\DropDownNavigation.ascx
Details: Request for the permission of type Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c failed.

Like the other posters, I've looked elsewhere, but I don't know what's up. Anybody help??

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, June 29, 2004 10:20 PM by hgs

Add control to the web.config of your sharepoint site.

<SafeControls>
<SafeControl Assembly="WeatherInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6185e98411448c6a" Namespace="WeatherInfo" TypeName="*" Safe="True"
/>
</SafeControls>

# Impersonating WebPart

Wednesday, June 30, 2004 2:08 PM by TrackBack

# YASPQ

Sunday, January 30, 2005 12:03 PM by TrackBack

# re: SharePoint, Code Access Security and the SmartPart

Saturday, September 09, 2006 1:51 AM by leh

Hi all,

You can fix this error "

unable to load ~\UserControls\DropDownNavigation.ascx

Details: Request for the permission of type Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c failed"

by modifing your customs policyfile:

 <CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust">

             <IMembershipCondition version="1" AssemblyVersion="1.0.0.0" Name="SmartPart" class="StrongNameMembershipCondition" PublicKeyBlob="0x0024000004800000940000000602000000240000525341310004000001000100BB20A2F20001218C2100806C4A656705F4220FE7636574B9B8128E0AA44BE734F379B88130A29020D658ED8CFA1B0A0C93094A22144C54476A507FBB5D812C94DD412AB260EED31D99857A53E20200AC9020007D852540DB43C54D5269507DD45181B1B5E6766C9E2FB3DD0C931C2D1B22ADFDCEC8FDB02E423E3D8BBD59C4B9" />

           </CodeGroup>

If you still can not fix this, send request to my (hlhamduc@yahoo.com), i will sent to you my customs policy file

# re: SharePoint, Code Access Security and the SmartPart

Wednesday, January 31, 2007 3:04 PM by rsteeno

Great web part!

In issue i'm having is on a site that is using Forms authentication with anonymous access.  Pages that do not have an instances of the smart part load fine.  as soon as I drop an instance of the smart part on the page, i get redirected to the site login page.

It seems to be some type of security issue with the UserControls folder, but I have not been able to figure it out.  Anyone else out there using the smartpart with anonymous access??

Thanks!

# re: SharePoint, Code Access Security and the SmartPart

Friday, February 23, 2007 7:32 PM by maricoqf

Good job...<a href= http://www2.sae.edu/me/ext/2/Viagra-in-Mexico.html >Viagra in Mexico</a> [url=http://www2.sae.edu/me/ext/2/Viagra-in-Mexico.html]Viagra in Mexico[/url] <a href= http://www2.sae.edu/me/ext/2/Viagra-50mg.html >Viagra 50mg</a> [url=http://www2.sae.edu/me/ext/2/Viagra-50mg.html]Viagra 50mg[/url]  

# re: SharePoint, Code Access Security and the SmartPart

Thursday, March 15, 2007 11:46 PM by Jaya

I am stuck in am major issue.

I have a development virtual server with Ms VS 2003 and wss, after completion of code , now I have migrated this site to another virtual server which has only wss.

I am able to access normal webparts . But those webparts with Data Grids Excel exports are not able to work.

If I give anonymous access to the wss and iss, if can show these webparts also.

What configuration issues is causing this problem??

Please advise

# re: SharePoint, Code Access Security and the SmartPart

Monday, March 19, 2007 3:53 AM by ...

Stupore! ho una sensibilit molto buona circa il vostro luogo!!!!

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, March 20, 2007 6:23 PM by lindieitg

<a href=" http://www.xhttp.net/s1/viagra.html ">viagra</a>

# re: SharePoint, Code Access Security and the SmartPart

Saturday, March 24, 2007 7:39 AM by Lucy! Please call me,Jonny

Lucy! Please call me,Lucy! Please call me

# re: SharePoint, Code Access Security and the SmartPart

Wednesday, April 04, 2007 11:48 AM by Frank Walsh

I was able to get my web part working by adding it to the web.config compilation tag as suggested, which stopped the partially trusted assembly message I was receiving. However, now I receive a message simply stating there was an error, I believe this has something to do with the 3rd party control I'm attempting to use but I'd like to dive further into it, is there somewhere I can get more detailed information about the error?

THanks,

Frank.Walsh[at]gmail.com

# re: SharePoint, Code Access Security and the SmartPart

Sunday, April 22, 2007 5:40 PM by Britneysrltc

<a href= http://7.fresnewsrch.info >a yiddishe mamma lyrics</a> <a href= http://8.fresrealpost.info >a800 cable data samsung</a> <a href= http://5.fresrealfish.info >a diagram of the human body</a> <a href= http://3.fresrealapple.info >aasvogel skip</a> <a href= http://4.fresnewsrch.info >aascif</a>

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, April 24, 2007 3:02 AM by Britneybwmdy

<a href= http://www.angelfire.com/funky/qociso >a174 admed ron</a> <a href= http://www.angelfire.com/indie/berofe >aa2219</a> <a href= http://www.angelfire.com/hiphop/kutoge >a950 cable sch usb</a> <a href= http://www.angelfire.com/funky/nawudo >aaron woodard</a> <a href= http://www.angelfire.com/planet/xonuxa >a1200 moto</a>

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, April 24, 2007 3:02 AM by Britneybwmdy

<a href= http://www.angelfire.com/funky/qociso >a174 admed ron</a> <a href= http://www.angelfire.com/indie/berofe >aa2219</a> <a href= http://www.angelfire.com/hiphop/kutoge >a950 cable sch usb</a> <a href= http://www.angelfire.com/funky/nawudo >aaron woodard</a> <a href= http://www.angelfire.com/planet/xonuxa >a1200 moto</a>

# re: SharePoint, Code Access Security and the SmartPart

Thursday, April 26, 2007 12:07 AM by Britneyzkbat

<a href= http://www.angelfire.com/crazy/hyrebi >aa theory tests</a> <a href= http://www.angelfire.com/poetry/qyvaca >a.h ferguson poetry</a> <a href= http://www.angelfire.com/blog/zuhoto >a tooth cavity</a> <a href= http://www.angelfire.com/hiphop/pohixu >aaatravelagency.com</a> <a href= http://www.angelfire.com/droid/wekyne >a.a big book</a>

# re: SharePoint, Code Access Security and the SmartPart

Friday, April 27, 2007 1:07 AM by Britneyjmwxt

<a href= http://www.angelfire.com/indie/budewi >a printable metric system chart</a> <a href= http://www.angelfire.com/hiphop/suwawo >a1 limousine princeton nj</a> <a href= http://www.angelfire.com/droid/xoraza >aaron hamill man show</a> <a href= http://www.angelfire.com/punk/sycimy >a little too late</a> <a href= http://www.angelfire.com/punk/nivodo >a href http hclient.chat.yahoo.c</a>

# re: SharePoint, Code Access Security and the SmartPart

Friday, April 27, 2007 4:50 PM by RaymondLee

Why is it that the web part that I wrote to host a user control cannot recognize ~\UserControls\ unless I run page with smartpart on it first. Do you have code that sets the ~\UserControls\ directory somehow to be recognized by sharepoint ? I get this message,

Error: unable to load ~\UserControls\UCContestEntry.ascx

Details: Request failed.

but it does work if I use  a page with smartpart on it before I use this page. Thanks!

# re: SharePoint, Code Access Security and the SmartPart

Saturday, April 28, 2007 8:54 PM by Britneyuyiii

<a href= http://www.angelfire.com/planet/bequhe >a ti mesmo</a> <a href= http://www.angelfire.com/goth/saveze >a shadow</a> <a href= http://www.angelfire.com/funky/fycane >a long obedience</a> <a href= http://www.angelfire.com/droid/vojati >aa meeting topic</a> <a href= http://www.angelfire.com/indie/mefaku >a20m ibm think pad is not starting</a>

# re: SharePoint, Code Access Security and the SmartPart

Saturday, April 28, 2007 8:54 PM by Britneyiikrr

<a href= http://www.angelfire.com/indie/jakedi >a rising tide all ships</a> <a href= http://www.angelfire.com/droid/covaco >a.i.m</a> <a href= http://www.angelfire.com/hiphop/hizoto >a credit score of</a> <a href= http://www.angelfire.com/crazy/jisipy >a consensus</a> <a href= http://www.angelfire.com/crazy/vewazy >aaliyah dated jay z</a>

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, May 08, 2007 8:55 PM by Britneyyrbtd

<a href= http://www.angelfire.com/goth/quvoma >lanparty ut nf3 250gb bios</a> <a href= http://www.angelfire.com/goth/bugizi >noble horse theatre</a> <a href= http://www.angelfire.com/goth/galela >spanish stuffed potato recipes</a> <a href= http://www.angelfire.com/punk/sitadu >paris beacon news</a> <a href= http://www.angelfire.com/funky/lunela >enviromental article</a>

# re: SharePoint, Code Access Security and the SmartPart

Tuesday, May 08, 2007 8:55 PM by Britneyyrbtd

<a href= http://www.angelfire.com/goth/quvoma >lanparty ut nf3 250gb bios</a> <a href= http://www.angelfire.com/goth/bugizi >noble horse theatre</a> <a href= http://www.angelfire.com/goth/galela >spanish stuffed potato recipes</a> <a href= http://www.angelfire.com/punk/sitadu >paris beacon news</a> <a href= http://www.angelfire.com/funky/lunela >enviromental article</a>

# re: SharePoint, Code Access Security and the SmartPart

Friday, May 11, 2007 1:03 AM by Britneyelsid

<a href= http://www.angelfire.com/blog/begequ >benefits.com book guest mart wal</a> <a href= http://www.angelfire.com/poetry/hakonu >elipsis band</a> <a href= http://www.angelfire.com/planet/hifado >1980 bulldozers</a> <a href= http://www.angelfire.com/indie/herana >alberta learners permit test</a> <a href= http://www.angelfire.com/blog/muzovi >christmas coloring page tree</a>

# re: SharePoint, Code Access Security and the SmartPart

Wednesday, May 30, 2007 5:44 AM by Anyutka-1985

.  ,   ,     ,   ,   Wildhog,

 .      ,    ,  ,

      " "<a href=http://saylormooyen.justfree.com>.</a>

,  .

<a href=http://saylormooyen.justfree.com><img>http://saylormooyen.justfree.com/tm.jpg</img></a>

# re: SharePoint, Code Access Security and the SmartPart

Wednesday, May 30, 2007 6:33 PM by Stether

cigarettes.blogbugs.org - cigarettes blog

# re: SharePoint, Code Access Security and the SmartPart

Friday, June 08, 2007 6:31 AM by Buzzycode

All the best!!!!

Free Online Php tutorials

http://www.buzzycode.com offers free online PHP tutorials. Best suited for beginners.

Several sample projects are available for download in this site. Learn Php step by step. Many senior Php professionals answer the technical questiosn by users. Visit http://www.buzzycode.com.

# re: SharePoint, Code Access Security and the SmartPart

Monday, July 09, 2007 2:37 AM by Saroj

Thanks Jan!

For such useful SmartPart articles.

I too faced the similar security issues when i tired to access

the List and doc. Lib. features through my user controls.

But it was sorted out when deployed in GAC and referred in web.config <assemblies> tag.

Thanks a lot!