Click here to Skip to main content
15,915,789 members
Home / Discussions / C#
   

C#

 
QuestionCheck Windows username and password using C#.Net 2003 Pin
Dotnetkanna30-Apr-07 21:30
Dotnetkanna30-Apr-07 21:30 
AnswerRe: Check Windows username and password using C#.Net 2003 Pin
Christian Graus30-Apr-07 22:25
protectorChristian Graus30-Apr-07 22:25 
GeneralRe: Check Windows username and password using C#.Net 2003 Pin
Colin Angus Mackay30-Apr-07 22:30
Colin Angus Mackay30-Apr-07 22:30 
GeneralRe: Check Windows username and password using C#.Net 2003 Pin
Dotnetkanna30-Apr-07 23:40
Dotnetkanna30-Apr-07 23:40 
GeneralRe: Check Windows username and password using C#.Net 2003 Pin
Dotnetkanna30-Apr-07 23:39
Dotnetkanna30-Apr-07 23:39 
GeneralRe: Check Windows username and password using C#.Net 2003 Pin
Christian Graus30-Apr-07 23:59
protectorChristian Graus30-Apr-07 23:59 
GeneralRe: Check Windows username and password using C#.Net 2003 Pin
Dotnetkanna1-May-07 0:17
Dotnetkanna1-May-07 0:17 
AnswerRe: Check Windows username and password using C#.Net 2003 Pin
MrEyes1-May-07 2:37
MrEyes1-May-07 2:37 
While I am not sure this will fit what you need I do something similar in one of the applications I develop and you may be able to tweak this.

Basically this application has functionality within it that could feasible be very destructive in the wrong hands. As a result of this I need to ensure that the user sat at the machine is the actual user that is logged on. In order to do this when the application opens it prompts them to enter their windows authentication information.

Internally the code uses the following:

const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_PROVIDER_DEFAULT = 0;

[DllImport("advapi32.dll", SetLastError=true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

private void DoLogonCheck()
{
  this.logonAttemptCount++;
  IntPtr phToken = IntPtr.Zero;

  bool logonRes = LogonUser(
    txtDomainUserName.Text,
    txtDomainName.Text,
    txtDomainPassword.Text, 
    LOGON32_LOGON_INTERACTIVE,
    LOGON32_PROVIDER_DEFAULT,
    ref phToken);

  if (logonRes)
  {
    this.DialogResult = DialogResult.OK;
    this.domainUserName = txtDomainUserName.Text;
    this.domainName = txtDomainName.Text;
  }
  else if ((!logonRes) && (this.logonAttemptCount < this.maxLogonAttempts))
  {
    //show message box with logon fail message

    txtDomainPassword.Text = string.Empty;
    txtDomainPassword.Focus();
  }
  else if ((!logonRes) && (this.logonAttemptCount >= this.maxLogonAttempts))
  {
    //show message box advising to many failures, app will quit

    System.Windows.Forms.Application.Exit();
  }
}


Externally to this method the code also checks that the given user name is the same user that is under the Environment.UserName object.

As an additional level of security the form that results from successful authentication has a timeout that will close it after X seconds of inactivity. This means that if the user logs in and then gets up an walks away the application isnt open to any passer by.

In theory I believe that this code could also be used to check the logon for a user that is not currently logged on, however I have never used it that way so I cannot say for certain
GeneralRe: Check Windows username and password using C#.Net 2003 Pin
Dotnetkanna1-May-07 19:41
Dotnetkanna1-May-07 19:41 
GeneralRe: Check Windows username and password using C#.Net 2003 Pin
MrEyes1-May-07 22:40
MrEyes1-May-07 22:40 
QuestionHow can I use Formula in crystal Report 9 ? Pin
mahmoud wafy30-Apr-07 21:28
mahmoud wafy30-Apr-07 21:28 
Questionunrecognized zeros in C# string format Pin
zoroyster30-Apr-07 20:32
zoroyster30-Apr-07 20:32 
AnswerRe: unrecognized zeros in C# string format Pin
Christian Graus30-Apr-07 21:00
protectorChristian Graus30-Apr-07 21:00 
QuestionC# and C++ class in dll Pin
Levnsn30-Apr-07 19:57
Levnsn30-Apr-07 19:57 
AnswerRe: C# and C++ class in dll Pin
Christian Graus30-Apr-07 20:45
protectorChristian Graus30-Apr-07 20:45 
Questionwindow forms Pin
kalyan_241630-Apr-07 18:29
kalyan_241630-Apr-07 18:29 
AnswerRe: window forms Pin
Kashif Iqbal Khan30-Apr-07 19:49
Kashif Iqbal Khan30-Apr-07 19:49 
AnswerRe: window forms Pin
Christian Graus30-Apr-07 20:58
protectorChristian Graus30-Apr-07 20:58 
Questioninstance does not exist in the current context Pin
Latheesan30-Apr-07 17:50
Latheesan30-Apr-07 17:50 
AnswerRe: instance does not exist in the current context Pin
Latheesan30-Apr-07 17:53
Latheesan30-Apr-07 17:53 
AnswerRe: instance does not exist in the current context Pin
andreshs130-Apr-07 19:51
andreshs130-Apr-07 19:51 
AnswerRe: instance does not exist in the current context Pin
Christian Graus30-Apr-07 22:26
protectorChristian Graus30-Apr-07 22:26 
GeneralRe: instance does not exist in the current context Pin
Latheesan1-May-07 5:43
Latheesan1-May-07 5:43 
GeneralRe: instance does not exist in the current context Pin
Christian Graus1-May-07 10:40
protectorChristian Graus1-May-07 10:40 
AnswerRe: instance does not exist in the current context Pin
Latheesan1-May-07 6:03
Latheesan1-May-07 6:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.