Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can C # modify security permissions through permission classes and permission properties

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how C # modifies security permissions through permission classes and permission properties". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how C # modifies security permissions through permission classes and permission properties".

Using System;using System.Security;using System.Security.Permissions;using System.Runtime.InteropServices;class NativeMethods {[DllImport ("msvcrt.dll")] public static extern int puts (string str); [DllImport ("msvcrt.dll")] internal static extern int _ flushall ();} class MainClass {private static void CallUnmanagedCodeWithoutPermission () {SecurityPermission perm = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode); perm.Deny () Try {Console.WriteLine ("Attempting to call unmanaged code without permission."); NativeMethods.puts ("Hello World!"); NativeMethods._flushall (); Console.WriteLine ("Called unmanaged code without permission. Whoops! ");} catch (SecurityException) {Console.WriteLine (" Caught SecurityException attempting to call unmanaged code. ");}} private static void CallUnmanagedCodeWithPermission () {SecurityPermission perm = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode); perm.Assert (); try {Console.WriteLine (" Attempting to call unmanaged code with permission. ") NativeMethods.puts ("Hello World!"); NativeMethods._flushall (); Console.WriteLine ("Called unmanaged code with permission.");} catch (SecurityException) {Console.WriteLine ("Caught SecurityException attempting to call unmanaged code. Whoops! ");}} public static void Main () {SecurityPermission perm = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode); perm.Assert (); CallUnmanagedCodeWithoutPermission (); perm.Deny (); CallUnmanagedCodeWithPermission ();}} using System;using System.Security;using System.Security.Permissions;using System.Runtime.InteropServices Class NativeMethods {[DllImport ("msvcrt.dll")] public static extern int puts (string str); [DllImport ("msvcrt.dll")] internal static extern int _ flushall ();} class MainClass {[SecurityPermission (SecurityAction.Deny, Flags = SecurityPermissionFlag.UnmanagedCode)] private static void CallUnmanagedCodeWithoutPermission () {try {Console.WriteLine ("Attempting to call unmanaged code without permission.") NativeMethods.puts ("Hello World!"); NativeMethods._flushall (); Console.WriteLine ("Called unmanaged code without permission. Whoops! ");} catch (SecurityException) {Console.WriteLine (" Caught SecurityException attempting to call unmanaged code. ");}} [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.UnmanagedCode)] private static void CallUnmanagedCodeWithPermission () {try {Console.WriteLine (" Attempting to call unmanaged code with permission. ") NativeMethods.puts ("Hello World!"); NativeMethods._flushall (); Console.WriteLine ("Called unmanaged code with permission.");} catch (SecurityException) {Console.WriteLine ("Caught SecurityException attempting to call unmanaged code. Whoops! ");}} public static void Main () {SecurityPermission perm = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode); perm.Assert (); CallUnmanagedCodeWithoutPermission (); perm.Deny (); CallUnmanagedCodeWithPermission ();}} using System;using System.Security;using System.Security.Permissions;using System.Runtime.InteropServices Class NativeMethods {[SuppressUnmanagedCodeSecurityAttribute ()] [DllImport ("msvcrt.dll")] internal static extern int puts (string str); [SuppressUnmanagedCodeSecurityAttribute ()] [DllImport ("msvcrt.dll")] internal static extern int _ flushall ();} class MainClass {[SecurityPermission (SecurityAction.Deny, Flags = SecurityPermissionFlag.UnmanagedCode)] private static void CallUnmanagedCodeWithoutPermission () {try {UIPermission uiPermission = new UIPermission (PermissionState.Unrestricted) UiPermission.Demand (); Console.WriteLine ("Attempting to call unmanaged code without UnmanagedCode permission."); NativeMethods.puts ("Hello World!"); NativeMethods._flushall (); Console.WriteLine ("Called unmanaged code without UnmanagedCode permission.");} catch (SecurityException) {Console.WriteLine ("Caught SecurityException attempting to call unmanaged code.") }} [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.UnmanagedCode)] private static void CallUnmanagedCodeWithPermission () {try {Console.WriteLine ("Attempting to call unmanaged code with permission."); NativeMethods.puts ("Hello World!"); NativeMethods._flushall (); Console.WriteLine ("Called unmanaged code with permission.") } catch (SecurityException) {Console.WriteLine ("Caught SecurityException attempting to call unmanaged code. Whoops!);}} public static void Main () {SecurityPermission perm = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode); perm.Assert (); CallUnmanagedCodeWithoutPermission (); perm.Deny (); CallUnmanagedCodeWithPermission ();}} so far, I believe you have a better understanding of "how C # modifies security permissions through permission classes and permission properties". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report