开发者

Dotnet :- How to achieve windows authentication in window form application?

开发者 https://www.devze.com 2023-02-16 22:21 出处:网络
I want to make a windows form application and want to use windows authentication to log in the user, it has to be used in intranet. the applc开发者_运维知识库ation should accept the user name and pass

I want to make a windows form application and want to use windows authentication to log in the user, it has to be used in intranet. the applc开发者_运维知识库ation should accept the user name and password from user and should authenticate it. how to achieve this.


You can achieve this using Interop Services. Use 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;
    }


Environment.UserName gives you the username of the current user. A password is not needed since the user have logged into windows.

Alternative: WindowsIdentity.GetCurrent()


Please refer the following link to apply windows Authentication in Intranet:

http://msdn.microsoft.com/library/bb882216.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消