发行:验证用户登录名和密码 [英] Issue on verifying user login name and password

查看:163
本文介绍了发行:验证用户登录名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们我需要需要验证有效的用户登录才能使用软件的软件。我想这样的:

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 );
 }

我使用的.NET Framework 4。在这上面code如何获得域名的计算机。 我努力了 SystemInformation.ComputerName SystemInformation.UserDomainName 但我得到的错误是:

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 :

服务器无法联系

。 我们可以得到使用在C#中的头文件,当前的用户名和密码? 请回答。 编辑: 我用这在我的电脑或AD本地登录。

. 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.

推荐答案

有没有办法让用户的密码。这可能导致各种安全问题。如果你想验证超出了机器已经从Windows知道凭据登录你要问的用户输入密码呢。

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.

下面code对我的作品在许多领域一片森林。如果你不指定在 PrincipalContext 构造域把它连接到它的运行对计算机的域。在我的测试中也不要紧,如果你正在验证用户在不同的领域,只要在域之间存在林中相应的信任关系。

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);
}

您可以得到一个对象,再presenting当前用户是这样的:

You can get an object representing the current user like this:

var user = System.Security.Principal.WindowsIdentity.GetCurrent();

更新

如果你想检查凭据对计算机上的本地帐户数据库,然后只是改变类型的 PrincipalContext

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);
}

这篇关于发行:验证用户登录名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆