Giving a program permisions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • velocius
    New Member
    • Oct 2006
    • 8

    Giving a program permisions

    Ok, the title might not be 100% clear, but i'll be more precise here

    When i attempt to read an XML into an Dataset, i use the following line of code

    Dataset1.ReadXm l("C:\test.xml" );

    This works fine on my portable, but if i use it on a second PC, i get the following error.

    Security Exception Was Unhandled
    Request for the permission of type System.Security .Permissions.Fi leIOPermission, ... (this contains the public key, wich isn't too important for you guys i think)

    if you didn't know, the language is C#, .NET framework 2.0.

    I suspect it might be the permissions, i have the administrator name and pasword, is it possible to give my program those as well?

    or is there another problem thats causing this?

    any help so i can come to a solution would be apriciated :)
  • velocius
    New Member
    • Oct 2006
    • 8

    #2
    I tried using Impersonating a diffrent perrson, giving the login and pasword of the admin, but i get an error, stating i don't have permision (to change my permisions)


    Code:
    //impersonate admin rights
                Impersonator i = new Impersonator("Administrator", "Domain", "Pasword");
                i.Impersonate();
                //this the the location of the crystal report, if the report changes location, this needs to be updated
                MyReport.Load("\\\\srv3\\home$\\programmas\\IntoToCr\\IntoToCr\\Arrival.rpt");
                //Stop using admin rights
                i.Undo();

    The problem lies at this.impersonat ionContext = this.Logon().Im personate();
    wich i have made bold in the code of Impersonator.cs

    I hope someone has a clue to a solution.

    Impersonator.cs

    Code:
    using System;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Security;
    using System.Security.Permissions;
    namespace test
    {
        /// <summary>
        ///		Impersonator class allows client code to impersonate another domain user account 
        ///		by handling underlying account authentication and security context manipulation
        /// </summary>
        public class Impersonator
        {
            // private members for holding domain user account credentials
            private string username = String.Empty;
            private string password = String.Empty;
            private string domain = String.Empty;
            // this will hold the security context for reverting back to the client after impersonation operations are complete
            private WindowsImpersonationContext impersonationContext = null;
    
            // disable instantiation via default constructor
            private Impersonator()
            { }
    
            /// <summary>
            /// The constructor, sets the username, domain and pasword
            /// </summary>
            /// <param name="username">Username with enough rigths</param>
            /// <param name="domain"></param>
            /// <param name="password">Password of the user</param>
            public Impersonator(string username, string domain, string password)
            {
                // set the properties used for domain user account
                this.username = username;
                this.domain = domain;
                this.password = password;
            }
    
            private WindowsIdentity Logon()
            {
                IntPtr handle = new IntPtr(0);
                handle = IntPtr.Zero;
    
                const int LOGON32_LOGON_NETWORK = 3;
                const int LOGON32_PROVIDER_DEFAULT = 0;
    
                // attempt to authenticate domain user account
                bool logonSucceeded = LogonUser(this.username, this.domain, this.password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, ref handle);
    
                if (!logonSucceeded)
                {
                    // if the logon failed, get the error code and throw an exception
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new Exception("User logon failed. Error Number: " + errorCode);
                }
    
                // if logon succeeds, create a WindowsIdentity instance
                WindowsIdentity winIdentity = new WindowsIdentity(handle);
    
                // close the open handle to the authenticated account
                CloseHandle(handle);
    
                return winIdentity;
            }
    
            /// <summary>
            /// authenticates the domain user account and begins impersonating it
            /// </summary>
            public void Impersonate()
            {
               [B] this.impersonationContext = this.Logon().Impersonate();[/B]        }
    
            /// <summary>
            /// rever back to original security context which was stored in the WindowsImpersonationContext instance
            /// </summary>
            public void Undo()
            {
                // 
                this.impersonationContext.Undo();
            }
    
            [DllImport("advapi32.dll", SetLastError = true)]
            private static extern bool LogonUser(string lpszUsername,
                                                string lpszDomain,
                                                string lpszPassword,
                                                int dwLogonType,
                                                int dwLogonProvider,
                                                ref IntPtr phToken);
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            private static extern bool CloseHandle(IntPtr handle);
    
        }
    }

    Comment

    • scripto
      New Member
      • Oct 2006
      • 143

      #3
      ummmm, try giving permissions to the folder where the file is kept.

      Comment

      Working...