Re: Custom Security Permission & Security Attribute troubles
From: Dominick Baier [DevelopMentor] (dbaier_at_pleasepleasenospamdevelop.com)
Date: 09/09/05
- Next message: Angelos Karantzalis: "Re: Custom Security Permission & Security Attribute troubles"
- Previous message: Angelos Karantzalis: "Custom Security Permission & Security Attribute troubles"
- In reply to: Angelos Karantzalis: "Custom Security Permission & Security Attribute troubles"
- Next in thread: Angelos Karantzalis: "Re: Custom Security Permission & Security Attribute troubles"
- Reply: Angelos Karantzalis: "Re: Custom Security Permission & Security Attribute troubles"
- Reply: Nicole Calinoiu: "Re: Custom Security Permission & Security Attribute troubles"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Fri, 09 Sep 2005 03:55:39 -0700
Hello Angelos Karantzalis" akarantzalis[at]yahoo.com,
in addition to SNing, installing in the GAC, applying [Serializable] and
[AllowPartiallyTrustedCallers], you have to install the assembly containing
the permission and attribute in the "policy assemblies" list by using mscorcfg.msc
or programmatically - i have some code that describes the progammatic way
on my blog:
http://www.leastprivilege.com/PolicyAssembliesHeadaches.aspx
---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com
> Hi guys,
>
> I'm trying to write a library that will allow me to add a custom
> permission for my applications (let's call it ApplicationPermission
> for now), and a custom security attribute to support declarative
> syntax.
>
> I've gone about this, creating my ApplicationPermission class, derived
> from CodeAccessPermission, implementing the IUnrestrictedPermission as
> well. Marked Serializable also.
>
> I've derived my custom attribute from CodeAccessSecurityAttribute, in
> order to use the following declarative syntax:
> [ApplicationPermission(SecurityAction.Demand, ActionName="test",
> PrincType = PrincipalType.User, PrincipalNames = "username")]
>
> Now, my library compiles fine, but when I try to compile my little
> test app,
> I get an error like:
> "C:\projects\SecurityContextTest\Class1.cs(23): Error emitting
> 'SecurityContext.ApplicationPermissionAttribute' attribute --
> 'Unexpected
> exception processing attribute -- System.NullReferenceException:
> Object
> reference not set to an instance of an object..'"
> I'm including the code of my custom attribute here:
>
> using System;
> using System.Security;
> using System.Security.Permissions;
> namespace SecurityContext
> {
> /// <summary>
> /// ApplicationPermissionAttribute defines a declarative attribute to
> be
> used inside code that
> /// requires ApplicationPermissions defined declaratively.
> /// </summary>
> [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple
> =
> true)]
> public class ApplicationPermissionAttribute :
> CodeAccessSecurityAttribute
> {
> private string m_ActionName;
> private string[] m_PrincipalNames = null;
> private PrincipalType m_PrincipalType;
> public ApplicationPermissionAttribute(SecurityAction action) :
> base(action) {
> // Do nothing here ...
> }
> /// <summary>
> /// ActionName accessor
> /// </summary>
> public virtual string ActionName {
> get {
> return m_ActionName;
> }
> set {
> m_ActionName = value;
> }
> }
> /// <summary>
> /// PrincipalNames accessor
> /// </summary>
> public virtual string PrincipalNames {
> get {
> System.Text.StringBuilder sb = new
> System.Text.StringBuilder();
> foreach(string principal in m_PrincipalNames)
> sb.Append(principal+",");
> return sb.ToString();
> }
> set {
> m_PrincipalNames = value.Split(new char[] {','});
> }
> }
> /// <summary>
> /// PrincipalType accesssor (User,Role)
> /// </summary>
> public virtual PrincipalType PrincType {
> get {
> return m_PrincipalType;
> }
> set {
> m_PrincipalType = value;
> }
> }
> public new bool Unrestricted {
> get {
> return false;
> }
> set {
> // Do nothing
> }
> }
> public override IPermission CreatePermission(){
> Console.Out.WriteLine("Creating a new ApplicationPermission");
> return new ApplicationPermission(m_ActionName,
> m_PrincipalNames,
> m_PrincipalType);
> }
> }
> }
> .. now, originally I've went about by deriving from SecurityPermission
> & SecurityAttribute. However, although my two projects compiled ok,
> the attribute wasn't being emitted (just a guess), so the call to
> Demand() on my custom permission was never being made. That's why I
> switched to deriving from the CodeAccessSecurityPermission &
> CodeAccessSecurityAttribute, signed my assembly & put it in the GAC
> ... and all sorts of problems followed, until I've hit the wall with
> the one I've described in the beginning :(
>
> Is there anybody out there who could possibly shed some light on my
> dark
> coding day ?
> Cheers,
> Angel
> O:]
- Next message: Angelos Karantzalis: "Re: Custom Security Permission & Security Attribute troubles"
- Previous message: Angelos Karantzalis: "Custom Security Permission & Security Attribute troubles"
- In reply to: Angelos Karantzalis: "Custom Security Permission & Security Attribute troubles"
- Next in thread: Angelos Karantzalis: "Re: Custom Security Permission & Security Attribute troubles"
- Reply: Angelos Karantzalis: "Re: Custom Security Permission & Security Attribute troubles"
- Reply: Nicole Calinoiu: "Re: Custom Security Permission & Security Attribute troubles"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|