Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6087581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:53:11+00:00 2026-05-23T11:53:11+00:00

I created a CodeAccessSecurityAttribute implementation, witch use stack information to find the target class

  • 0

I created a CodeAccessSecurityAttribute implementation, witch use stack information to find the target class name, but in some classes the PrincipalPermition is not created, the system uses the previews one instead. What did I miss?

[ComVisible(true)]
[AttributeUsageAttribute(AttributeTargets.Constructor | AttributeTargets.Method, AllowMultiple = true, Inherited = false)] 
public sealed class MyPrincipalPermissionAttribute : CodeAccessSecurityAttribute
{
    public MyPrincipalPermissionAttribute(SecurityAction action) : base(action) { }

    public override IPermission CreatePermission()
    {
        if (Unrestricted)
            return new PrincipalPermission(PermissionState.Unrestricted);
        var stackTrace = new StackTrace();

        var fullnameArray = new List<String>();
        foreach (var frame in stackTrace.GetFrames())
        {
            try
            {
                var method = frame.GetMethod();
                if (method != null && method.ReflectedType.IsSubclassOf(typeof (BaseClass)))
                    fullnameArray.Add(method.ReflectedType.FullName);
            } catch {}
        }

        if (fullnameArray.Count() > 0)
            return new PrincipalPermission(null, fullnameArray[0], true);

        return new PrincipalPermission(PermissionState.Unrestricted);
    }
}

And the usage

public class MyClassCalledFirstWork: BaseClass
{
    [MyPrincipalPermission(SecurityAction.Demand)]
    public override void DoSomething()
    {
        return;
    }
}

public class MyClassCalledSecondDontWork: BaseClass
{
    [MyPrincipalPermission(SecurityAction.Demand)]
    public override void DoSomething()
    {
        return;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:53:12+00:00Added an answer on May 23, 2026 at 11:53 am

    This is from documentation for SecurityAttribute.CreatePermission ():

    "CreatePermission method creates a
    permission object that can then be
    serialized into binary form and
    persistently stored along with the
    SecurityAction in an assembly’s
    metadata."

    "At compile time,
    attributes convert security
    declarations to a serialized form in
    metadata. Declarative security data in
    metadata is created from the
    permission that this method returns
    that corresponds to this attribute."

    It looks like there can be only one permission object (of some type) corresponding with custom CodeAccessSecurityAttribute stored for a specific SecurityAction.
    When you check the IL for DoSomething methods you can see that they contain identical permission demands with role defined during the first CreatePermission call.

    .method public hidebysig virtual instance void 
            DoSomething() cil managed
    {
        .permissionset demand
                 = {class 'CustomSecurityPermission.Program+MyPrincipalPermissionAttribute, CustomSecurityPermission, Version=1.0.0.0, Culture=neutral' = {}}
        ...
    } // end of method MyClassCalledSecondDontWork::DoSomething
    
    .method public hidebysig virtual instance void 
            DoSomething() cil managed
    {
        .permissionset demand
                 = {class 'CustomSecurityPermission.Program+MyPrincipalPermissionAttribute, CustomSecurityPermission, Version=1.0.0.0, Culture=neutral' = {}}
        ...
    } // end of method MyClassCalledFirstWork::DoSomething
    

    An answer for your second question from comments is:
    Instead of using declarative CAS I would use imperative:

    public sealed class Security
    {
        public static IPermission CreatePermission()
        {
            var stackTrace = new StackTrace();
    
            var fullnameArray = new List<String>();
            foreach (var frame in stackTrace.GetFrames())
            {
                try
                {
                    var method = frame.GetMethod();
                    if (method != null && method.ReflectedType.IsSubclassOf(typeof(BaseClass)))
                        fullnameArray.Add(method.ReflectedType.FullName);
                }
                catch { }
            }
            if (fullnameArray.Count() > 0)
            {
                return new PrincipalPermission(null, fullnameArray[0]);
            }
            return new PrincipalPermission(PermissionState.Unrestricted);
        }
    }
    
    public class MyClassCalledFirstWork : BaseClass
    {
        public override void DoSomething()
        {
            Security.CreatePermission().Demand();
            return;
        }
    }
    
    public class MyClassCalledSecondDontWork : BaseClass
    {
        public override void DoSomething()
        {
            Security.CreatePermission().Demand();
            return;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created some class: class Base{ public: Base(int = 0); ~Base(); Base(Base&); Base(Derived&); //<-
I created a class and overridden the equals() method. When I use assertTrue(obj1.equals(obj2)) ,
I created a Rails application normally. Then created the scaffold for an event class.
I've created the following attribute: [Serializable] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited =
Created using Sphinx 0.6.5. I know Python's documentation uses reStructuredText , but it has
I created the following xaml: <Button x:Name=PriceButton> <Button.Template> <ControlTemplate> <Border x:Name=ButtonBorder CornerRadius=2 Background={StaticResource DarkReflectionBrush}
Created an extensive batch script program to handle some automated file management and printing
created a c# console project, which contains a console class (program.cs). i added a
I created a control where other developers can create an instance and use. There
I created a MSI installer by using Visual Studio 2008. I have some temp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.