Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core and on premise AD authentication

I'd like to try and use ASP.NET Core MVC or Web API at my workplace but we have just Active Directory to authentication and authorization. Is there any solution to solve it with an on premise AD or we have to change for Java? I know this question is not original but I'd like to get a simple answer to it, please.

like image 906
Sándor Hatvani Avatar asked Aug 08 '17 16:08

Sándor Hatvani


People also ask

What is difference between authentication and Authorization in ASP.NET Core?

Authentication is the process of determining a user's identity. Authorization is the process of determining whether a user has access to a resource. In ASP.NET Core, authentication is handled by the authentication service, IAuthenticationService, which is used by authentication middleware.


1 Answers

As of today, System.DirectoryServices is not available in ASP.NET Core yet. You can read more here.

In the meantime, you can use Novell.Directory.Ldap.NETStandard. For example,

public bool ValidateUser(string domainName, string username, string password)
{
    string userDn = $"{username}@{domainName}";
    try
    {
        using (var connection = new LdapConnection {SecureSocketLayer = false})
        {
            connection.Connect(domainName, LdapConnection.DEFAULT_PORT);
            connection.Bind(userDn, password);

            if (connection.Bound)
                return true;
        }
    }
    catch (LdapException ex)
    {
        // Log exception
    }
    return false;
}

Since it has too many moving pieces, I have created a sample project at GitHub.

like image 120
Win Avatar answered Oct 21 '22 00:10

Win