Friends I need to make a software that needs to verifies the valid user login in order to use software. I tried this:
bool valid = false;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain,domainname))
{
valid = context.ValidateCredentials( username, password );
}
I am using .net framework 4 . In this above code how to get domainname of the computer.
I have tried
SystemInformation.ComputerName
,SystemInformation.UserDomainName
but i got error as :
The server could not be contacted
.
And Can we get the current username and password using any header file in c# ??
Please answer.
Edits:
I am using this for local login in my computer or AD.
There's no way to get the user's password. That could lead to all sorts of security issues. And if you want to verify credentials beyond what the machine already knows from the Windows logon you need to ask the user for the password anyway.
The following code works for me in a forest with many domains. If you don't specify a domain in the PrincipalContext
constructor it connects to the domain of the computer it's running on. In my testing it doesn't matter if the user you're validating is in a different domain, as long as appropriate trusts exist between the domains in the forest.
bool valid = false;
using (var context = new PrincipalContext(ContextType.Domain))
{
valid = context.ValidateCredentials(username, password);
}
You can get an object representing the current user like this:
var user = System.Security.Principal.WindowsIdentity.GetCurrent();
Update
If you're wanting to check credentials against the local account database on the machine then just change the type of the PrincipalContext
:
bool valid = false;
using (var context = new PrincipalContext(ContextType.Machine))
{
valid = context.ValidateCredentials(username, password);
}
Make sure that you have .NET 3.5 Installed as well
bool valid = false;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
valid = context.ValidateCredentials( username, password );
}
First off, you don't want to use the domain name of the computer. You want to use the domain name of the user logged in to the computer. This is because some networks can have a trust, where the computer is on one domain, but users on another domain can also log in.
So, to get the domain name of the user currently logged in to windows, you can use this (VB.NET example, but easy enough to covert to C#):
Dim arr As String() = My.User.Name.Split("\")
If (arr.Length > 0) Then
Dim domainName As String = arr(0).ToString
Dim userName As String = arr(1).ToString.ToLower
End If
Now to validate the user's Windows login again before starting your app, you have to make this Windows API call:
Private 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 Boolean
Const LOGON32_LOGON_INTERACTIVE As Long = 2
Const LOGON32_LOGON_NETWORK As Long = 3
Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean
Dim token As IntPtr
Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token)
End Function