Autenticación de Windows en .NET

I have a .NET application (mix of C# and VB.NET) where I would like to display a Windows Login Dialog box (or my own dialog box) and authenticate the user using Windows Authentication. Per requirement, I need to ask the user to authenticate after AT LEAST a minute of being idle. I would prefer a .NET native way of doing Windows Authentication but interested in other ways...

preguntado el 01 de febrero de 12 a las 22:02

2 Respuestas

to authenticate a user, you can use the ValidateCredential método de la PrincipalContext. Make sure to add reference System.DirectoryServices.AccountManagement.

//If you are validating on a domain
PrincipalContext pcon = new PrincipalContext(ContextType.Domain);    
    //User is authenticated

If you're not validating against a domain, check other ContextType. You can also use other option to validate the credentials (the ContextOptions).

Respondido 31 Jul 13, 21:07

Although this sounds like the best way to do it - just found out these classes are not available to .NET 2.0. - Denis

Found the following and figured I'd add it for completion sake. I still like Gabriel's answer!

Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As LogonType, ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer
Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean

    Public Enum LogonType As Integer
        LOGON32_LOGON_BATCH = 4
        LOGON32_LOGON_UNLOCK = 7
    End Enum

    Public Function IsAuthenticated(ByVal Username As String, ByVal Password As String, Optional ByVal Domain As String = "") As Boolean
        Dim Token As New IntPtr
        LogonUser(Username, Domain, Password, LogonType.LOGON32_LOGON_INTERACTIVE, 0, Token)
        If Token.ToInt32 <> 0 Then Return True
    End Function

Respondido 02 Feb 12, 20:02

Why would you use unmanaged code when you can do everything with managed code? - Gabriel GM

Because the managed code is not available in .NET framework 2.0 - Denis

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.