![](/img/trans.png)
[英]C# post username/password to the login page using windows application
[英]using windows username password in C#
我如何在我的C#APP中使用Windows存儲的用戶名和密碼。 我正在尋找返回成功登錄的bool狀態的方法。 意思是:
bool isCredentialValid = CheckLogin(username, password);
所以根據bool值,我可以顯示正確的信息。
更新
class Program
{
static void Main(string[] args)
{
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
Console.WriteLine("Enter username : ");
string user = Console.ReadLine();
Console.WriteLine("Enter password : ");
string pass = Console.ReadLine();
bool isValid = pc.ValidateCredentials(user, pass);
Console.WriteLine("State : {0}",isValid);
Console.ReadLine();
}
}
這段代碼不起作用。 當我啟用Guest帳戶時,它會在每次登錄時顯示為true,當我禁用Guest帳戶時,它甚至不會驗證現有帳戶。 任何幫助,將不勝感激。
看看PrincipalContext.ValidateCredentials方法。 例如,
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
bool isCredentialValid = pc.ValidateCredentials(username, password);
對於本地帳戶,請使用ContextType.Machine。
另一種方法是使用win32 api LogonUser功能
using System.Security.Principal;
添加using System.Security.Principal;
//for current Username full
string str = WindowsIdentity.GetCurrent().Name;
然后你可以使用這個用戶名進行身份驗證 對於密碼,它是不同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.