Search code examples
c#windows-authenticationblazor-server-sideasp.net-core-5.0

Blazor - Windows Authentication


I designing an in-house app that requires user authenticate against the AD. With that said, user has to provide his/her id/password at the login screen in order for the authentication. (We don't want anyone can just open the app and do whatever)

I have gone through several articles about setting up the environment and use the existing identity via System.Security.Principal.WindowsIdentity.GetCurrent(). No one talks about authenticating via information provided by the UI.

I have the login form built and I have a customized AuthenticationStateProvider, but I don't know how to pass the credential that I got from the user to Windows, so it can authenticate it with AD.

Can someone shed some light that as how I can go about doing this? Thanks!


Solution

  • I'm using the .NET Standard LDAP client library for that, it works fine.

    Use it like this:

    using (var cn = new LdapConnection())
    {
         // connect to AD host
         cn.Connect("your_ad", 389);
         try
         {
             cn.Bind("user@domain", "pwd");
         }
         catch(LdapException e)
         {
            // invalid credentials
         }
    }