Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
An exception of type logon failure:unknown username or bad password is raised if I use username and password for System.Diagnostics.Process using
C#
static void Main(string[] args)
        {
            Process p=new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = @"D:\2011 HOLIDAYS.pdf";
            p.StartInfo.UserName = "hello";
            SecureString ss = new SecureString();
            foreach (char c in "hello")
                ss.AppendChar(c);
            p.StartInfo.Password = ss;
            p.Start();//here exception is raised;
        } 
Posted
Updated 29-Mar-11 21:00pm
v3
Comments
Sandeep Mewara 30-Mar-11 2:22am    
Use PRE tags to format code part. It makes the question readable.

I think you have the wrong idea about the UserName and Password feature of Process.StartInfo: they do not relate to the PDF file - they cause the process to be run logged in as a different user. So, unless you have a user on your machine called "hello" with a login password of "hello", this isn't going to work. You also need to specify the domain to log the user into - see MSDN[^] for info.

If you are trying to open a password protected PDF, then there is likely to be a command line option to specify that when you start the process, but that will depend on the reader being used.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Mar-11 4:37am    
You're absolutely right; I added it in my answer (under [EDIT]) only later, when I noticed it is not executable file. My 5.
--SA
myes 30-Mar-11 4:59am    
This code is added just to illustrate the exception not to open the password protected pdf's.the same exception getting arised in my code which they have used password,username.hence just tried a small code with pdf and not to open password protected
OriginalGriff 30-Mar-11 5:12am    
Ah. Sorry! Did you set the domain?
myes 30-Mar-11 5:26am    
if i set username,password,domain as windows crendials and working directory never set it takes the default.Again exception of type arises below.
The specified executable is not a valid Win32 application.
OriginalGriff 30-Mar-11 5:41am    
Show us the real code fragment: the latest exception is different from your original post.
Since when the SecureString build on the user name string makes the password?! Use debugger and test ss. Before calling p.Start is should be your password, one-to-one, without any "security" :-).

You probably totally misunderstood the purpose of SecureString. Read properly: http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx[^]. There is a sample with the password on this page.

[EDIT]
I think I know the problem: this is not executable file but data file, a PDF. A user name and password are not applicable to the document. If the document is password-protected, this is different username/password, not those you pass to Process.

—SA
 
Share this answer
 
v3
Comments
myes 30-Mar-11 4:18am    
I tried with the sample used there with the given link .There also the exception arises logon failure :unknown username or bad password.
Sergey Alexandrovich Kryukov 30-Mar-11 4:29am    
OK, did you do what I suggested with debugger? 1) start with just hard-coding username and password, 2) using debugger (always use debugger if you have any problems) stop at p.Start and check p for correct password and user name, 3) if it does not work, make sure you can start with the same username/password starting from shell.
--SA
OriginalGriff 30-Mar-11 4:35am    
Morning SA! I suspect that he wants to open a passworded PDF, and doesn't realize what StartInfo.UserName is for... :laugh:
Sergey Alexandrovich Kryukov 30-Mar-11 4:40am    
Exactly. This is so funny. I did not even look at the file name at first. I only noticed it while answering OP's follow-up comment...
--SA

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