WinForms项目中的Windows身份验证 [英] Windows authentication in WinForms project

查看:62
本文介绍了WinForms项目中的Windows身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正试图在我的C#表单项目中找到一种提供Windows身份验证的方法。我找到了一些教程,包括一个关于代码项目的教程,但它们无法以一种基本的方式满足我的精确要求:它们都是通过测试文本框用户名/密码来对照当前记录的在用户中。我的问题是我希望能够验证具有有效Windows帐户的任何用户。这样做的原因是我想将我的项目中的某些管理功能限制为某些已知的个人,我想通过测试他们的Windows凭据来实现它,因为这意味着他们可以使用他们的常规工作站登录凭据而我没有关注我自己创建一个单独的,安全的凭证数据库。批准的个人不会登录到运行项目的机器 - 项目在标准用户帐户上运行,任何人都可以访问。



好​​的,所以我可以实施一个解决方案,让他们首先必须登录到目标工作站,然后使用我在这里和其他地方找到的常规解决方案。但是对于管理员来说这是一个额外的麻烦,如果可能的话我想避免。



这甚至可能吗?我首先认为它 主要是因为我们在工作中使用SVN来管理我们的代码而Tortoise SVN UI使用我们的标准Windows凭证对我们进行身份验证,无论是否可以,我都可以这样做我是机器上当前登录的用户。



如果有人能够指出我对这个问题的有用信息来源的方向我会非常感激它。



在此先感谢,



Brian

Hi everyone,

I am trying to find a way to provide windows authentication in my C# forms project. I have found a couple of tutorials, including one here on Code Project, but they fail to satisfy my precise requirements in one fundamental way: they all work by testing a text box username / password against the current logged in user. My problem is that I want to be able to authenticate any user that has a valid Windows account. The reason for this is that I want to restrict some administration functionality in my project to certain known individuals and I'd like to do it by testing their Windows credentials because it means they can use their regular workstation login credentials and I don't have to concern myself with creating a separate, secure credentials database. The approved individuals would not be logged in to the machine that the project is running on - the project runs on a standard user account and can be accessed by anyone.

Okay, so I could implement a solution that involves them having to log into the target workstation first and then use the regular solutions I've found on here and elsewhere. But it's an extra layer of hassle for the administrators that if possible I'd like to avoid.

Is this even possible? I am lead to believe that it is primarily because here at work we use SVN to manage our code and the Tortoise SVN UI authenticates us using our standard Windows credentials, and I can do that regardless of whether or not I am the currently logged in user on the machine.

If someone is able to point me in the direction of a useful information source for this problem I would very much appreciate it.

Thanks in advance,

Brian

推荐答案

尝试使用Interop服务。检查以下代码:



[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,3,0,ref tokenHandler) ;

返回isValid;

}
Try using Interop Services. check the below Code:

[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, 3, 0, ref tokenHandler);
return isValid;
}


这篇关于WinForms项目中的Windows身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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