Logon failu unknown user name or bad password

Hello,

Using VS 2005 Beta (VB) I would like to read a network share in the form of:
\\192.168.1.10\c$\ - Using the DirectoryInfo object. However when I try that I get the error:
Logon failure: unknown user name or bad password

How can I either popup a box for the user to type in the username or password or embed it into the program


Answer this question

Logon failu unknown user name or bad password

  • Davidmi

    I thought .NET 2.0 has a credentials class (im sure I saw it....)

  • Alex Wied

    what is the default username and password for the sql authentication (sql server express) !!!!!!!!!!!!!!!!!!!!

    please help !!!

  • Cameron Kloot

    Hi,

    You would need to store the logon credentials in some store and retrieve it and logon using the LogonUser Win32API call and then impersonate using the Identity.

    Code for creating identity and impersonating is provided below.




    [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 extern static bool CloseHandle(IntPtr handle);

    public static void CreateIdentity(string User, string Domain, string Password)
            {
                // The Windows NT user token.
                IntPtr tokenHandle = new IntPtr(0);
           
                const int LOGON32_PROVIDER_DEFAULT = 0;
                const int LOGON32_LOGON_NETWORK_CLEARTEXT = 3;

                tokenHandle = IntPtr.Zero;

               bool returnValue = LogonUser(User, Domain, Password,
                    LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT,
                    ref tokenHandle);
                if (false == returnValue)
                {
                    int ret = Marshal.GetLastWin32Error();
                    throw new Exception("LogonUser failed with error code: " +  ret);
                }
                WindowsIdentity id = new WindowsIdentity(tokenHandle);
                CloseHandle(tokenHandle);
                id.Impersonate();        }


     



    Regards,
    Vikram



  • ljenner01

    Hi,

    Converted it: Not tested though...
    ----------------------------------------------------------------------

    Imports System.Security.Principal
    Imports System.Runtime.InteropServices
    Public Class UserManager
        Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _
         ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, _
         ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer
        Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
        Public Shared Sub CreateIdentity(ByVal User As String, ByVal Domain As String, ByVal Password As String)
            Dim tokenHandle As New IntPtr(0)
            Dim ret As Integer
            Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
            Dim LOGON32_LOGON_NETWORK_CLEARTEXT As Integer = 3
            tokenHandle = IntPtr.Zero
            Dim returnValue As Boolean = LogonUser(User, Domain, Password, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, tokenHandle)
            If False = returnValue Then
                ret = Marshal.GetLastWin32Error()
                Throw New Exception("LogonUser failed with error code: " + ret)
            End If
            Dim id As New WindowsIdentity(tokenHandle)
            CloseHandle(tokenHandle)
            id.Impersonate()
        End Sub
    End Class

     

    ----------------------------------------------------------------------

    Regards,
    Vikram

  • Vikasumit

    Does anyone know how to do that in VB by chance

    PS - you would think it would be in the .net framework...

  • Gabo

    Username: sa

    There should be no password for a default configuration.

  • Karsten Januszewski

  • nick79

    I hope this helps others who received this error message.

    When I debugged my .net application on my local computer everything worked, however, when copying the same application to the server I received the error message Logon failure: Unknown user name or bad password All the proper permissions were set, still same error message.

    Then instead of copying the files to the web server, I published them using VS 2005...the error message went away. To publish: Build>>Publish Website


  • NRN

    Hi Vikram,

    I am trying to save file from one system to another system (connected in lan).
    Actually I am taking database backup through my application (.net).The database backup file will be stored in the system (in specified folder) where the SQL server is. Now I have to store the database backup file in another system connectend in LAN . While I am trying to do the same using File.copy("","") it is saying that "Login Failure: Unknown user name or bad password". Can U please help me in this regard. thanks in Advance

    Regards,
    Kiran


  • Logon failu unknown user name or bad password