[英].Net Directory services for local authentication
I am using directory services in .Net for first time.I want to use it to authenticate against local users on my machine in an asp.net website application The code is as follows 我是第一次使用.Net中的目录服务。我想使用它在asp.net网站应用程序中针对我的计算机上的本地用户进行身份验证。代码如下
DirectoryEntry entry = new DirectoryEntry("WinNT://"+Environment.MachineName+"/administrator", txtBoxUserName.Text, txtBoxPWD.Text);
For some reason I get an exception of "System.Runtime.InteropServices.COMException" although I used the right user name and password for administrator user. 由于某种原因,尽管我为管理员用户使用了正确的用户名和密码,但是却出现了“ System.Runtime.InteropServices.COMException”异常。
Any Help would be appreciated 任何帮助,将不胜感激
Try this: 尝试这个:
public bool IsAuthenticationValid(string userName, string password)
{
using (var context = new PrincipalContext(ContextType.Machine))
{
return context.ValidateCredentials(userName, password);
}
}
Edit: Forgot to mention you will need System.DirectoryServices and System.DirectoryServices.AccountManagement references. 编辑:忘记提及您将需要System.DirectoryServices和System.DirectoryServices.AccountManagement引用。
If you hit the same error, please describe what environment you are running this code in. By that I mean command line, web app, etc. Just some more background to see if I can help you further. 如果您遇到相同的错误,请描述运行此代码的环境。我的意思是命令行,Web应用程序等。仅提供一些背景知识,看看我是否可以进一步为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.