WinForms应用程序中的窗口身份验证 [英] Window Authentication in WinForms Applicaton
问题描述
全部,
我有一个可以在组织级别使用的Windows应用程序。所以我们想把Windows身份验证。所以我搜索并使用下面的代码。如何根据用户名和域验证用户。在下面的代码中,我们传递静态LogonType和LogonProvider。所以我想针对活动目录验证用户名和域。对于下面的代码我没有指定Active Directory路径,但我不知道它是如何工作的。
请提供一个解决方案来验证Active Directory中的用户名和域名服务器。
All,
I have an windows application which can be used in organization level. So we would like to put windows authentication. So I searched and used below code. How to verify the user based on the User name and domain. In the below code, we are passing static LogonType and LogonProvider. So I would like to validate the username and domain against the active directory. For the below code I did not specify the Active Directory path, but i dont know how its working.
Please give a solution to verify username and domain against Active Directory in the server.
[System.Runtime.InteropServices.DllImport("advapi32.dll")]
public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);
public bool IsValidateCredentials(string userName, string password, string domain)
{
IntPtr tokenHandler = IntPtr.Zero;
bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
return isValid;
}
private void button1_Click_1(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
bool isValid = IsValidateCredentials(txtUserName.Text, txtPassword.Text, txtDomain.Text);
if (isValid)
{
RadForm1 form = new RadForm1();
form.Show();
this.Hide();
}
else
{
lblMsg2.Text = isValid == true ? "Valid User details" : "Invalid User Details";
}
}
如果我不清楚,请告诉我......
谢谢,
Naveen
Please let me know if I am not clear...
Thanks,
Naveen
推荐答案
这是我用来验证用户的代码Active Direct登录。
This is the code I use verify a user has an Active Direct login.
Private Function ValidateActiveDirectoryLogin(ByVal Domain As String,
ByVal Username As String,
ByVal Password As String) As Boolean
' Connect to the domain.
Using oDomainInformation As New PrincipalContext(ContextType.Domain, Domain,
ContextOptions.Negotiate)
' Validate the credentials.
Return oDomainInformation.ValidateCredentials(Username, Password)
End Using
End Function
你应该可以使用此应用程序的标准Windows成员资格和角色提供程序。
在Visual Studio中选择您的项目,您应该在解决方案探索下有一个属性显示河禁用匿名身份验证并启用Windows身份验证。
这将修改您的web.config以使用网络服务器所属的域。
所有这一切真的是将以下内容添加到您的< system.web>条目:
You should be able to use the standard Windows membership and role provider for this application.
In Visual Studio select your Project and you should have a Properties display under your Solution Explorer. Disable Anonymous Authentication and enable Windows Authentication.
This will modify your web.config to use the domain that the webserver is a member of.
All this really does is add the following to your <system.web> entries:
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
这篇关于WinForms应用程序中的窗口身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!