Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get a token programitcally to access windows server's shared folder but i get this exception: The specified network password is not correct

This is the impersonation class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; // DllImport
using System.Security.Principal; // WindowsImpersonationContext
using System.Security.Permissions; // PermissionSetAttribute
namespace Impersonate
{
public class clsImpersonate
{
#region 'Impersonation'

// obtains user token
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);

public IntPtr ImpersonateUser(string sUsername, string sDomain, string sPassword)
{
// initialize token
IntPtr pExistingTokenHandle = new IntPtr(0);

// if domain name was blank, assume local machine
if (sDomain == "")
sDomain = System.Environment.MachineName;

try
{
string sResult = null;

const int LOGON32_PROVIDER_DEFAULT = 0;

const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;

// get handle to token
bool bImpersonated = LogonUser(sUsername, sDomain, sPassword,
LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);


// did impersonation fail?
if (false == bImpersonated)
{
int nErrorCode = Marshal.GetLastWin32Error();
sResult = "LogonUser() failed with error code: " + nErrorCode + "\r\n";

// show the reason why LogonUser failed
//MessageBox.Show(sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

// Get identity before impersonation
sResult += "Before impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n";

return pExistingTokenHandle;
}
catch (Exception ex)
{
throw ex;
}
}

public bool FreeImpersonationResource(IntPtr _token)
{
// close handle(s)
if (_token != IntPtr.Zero)
return CloseHandle(_token);
else return true;
}

#endregion
}
}
C#



And this where i am using instance of this class:
private void buttonLogon_Click(object sender, System.EventArgs e)
    {
        clsImpersonate cls = new clsImpersonate();

            IntPtr token = cls.ImpersonateUser(textBoxUsername.Text, textBoxDomain.Text, textBoxPassword.Text);

            using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(token))
            {
                string fileName = @"\\server\share\New Text Document.txt"; // the name of the sw you will use to open your file, I suppose MS Word

                Process process = new Process();
                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = fileName;
             //   info.Arguments = argument;

                process.StartInfo = info;
                process.Start();
            }


            cls.FreeImpersonationResource(token);
Posted
Comments
ZurdoDev 29-Sep-14 16:55pm    
What is your question? It seems like the error is pretty clear.
[no name] 30-Sep-14 0:37am    
The specified network password is not correct

1 solution

You can use slightly different variant of LogonUser


[^]

C#
IntPtr lnToken;

                   Console.WriteLine("User Before Impersonation : " + WindowsIdentity.GetCurrent().Name);

                   int TResult = LogonUser(ConfigurationManager.AppSettings["AccessUserName"].ToString(), ConfigurationManager.AppSettings["AccessDomain"].ToString(), ConfigurationManager.AppSettings["AccessPassword"].ToString(), LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, out lnToken);

                   if (TResult > 0)
                   {
                      
                           ImpersonateLoggedOnUser(lnToken);
Console.WriteLine("User After Impersonation : " + WindowsIdentity.GetCurrent().Name);
                           // DO STUFF
                            //........
                           //.........
                          RevertToSelf();
 
                   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900