验证本地计算机上的用户凭据 [英] Validate a users credentials on the local machine
问题描述
我有一个需要验证基于用户名和密码的用户,除了检查,如果用户所属的组WSMA Windows服务(运行作为本地系统用户)。我现在的code是这样的:
I have a Windows Service (running as the Local System user) that needs to validate a user based on username and password, in addition to checking if the user belongs to the group WSMA. My current code is like this:
var pc = new PrincipalContext(ContextType.Machine);
using (pc)
{
try
{
if (pc.ValidateCredentials(username, password))
{
using (var groupEntry = new DirectoryEntry("WinNT://./WSMA,group"))
{
foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
{
using (var memberEntry = new DirectoryEntry(member))
{
if (memberEntry.Path.ToLower().EndsWith(username.ToLower()))
{
return new LoginResult{ success = true };
}
}
}
}
}
return new LoginResult{ success = false };
}
catch (PrincipalOperationException poe)
{
if (poe.ErrorCode == -2147023688)
{
return new LoginResult { Success = false, ErrorMessage = "Password expired" };
}
throw poe;
}
}
这所有的作品,因为它应该的,只要我连接到网络,但如果我拔掉我的网线,然后ValidateCredentials打电话给我下面的错误信息:
This all works as it should, as long as I'm connected to the network, but if I plug out my network cable, then the ValidateCredentials call give me the following error message:
FileNotFoundException异常用户code unhandeled。网络路径没有被发现。的
FileNotFoundException unhandeled by user code. The network path was not found.
我想这已经是与广告,但我只需要检查的本地用户,所以不应该要求一个网络访问不是域用户。
I guess this has something to do with AD, but I only need to check the local users, and not domain users so a network access should not be required.
任何方式做到这一点使用PrincipalContext,或将在断开连接的情况下工作,一些其他的方式?
Any way to do this using the PrincipalContext, or some other way that will work in a disconnected scenario?
推荐答案
下面是登录用户(以及由此确认这是一个有效的用户名/密码)的方式:<一href=\"http://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext.aspx\"相对=nofollow> MSDN链接
Here's a way to logon the User (and thus check that it's a valid user/pass): MSDN Link
我想这应该工作断开也一样,如果你使用本地帐户
I guess this should work disconnected, too, if you use a local account
这篇关于验证本地计算机上的用户凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!