ASP.NET Core and on premise AD authentication

和自甴很熟 提交于 2019-11-30 03:50:04

问题


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.


回答1:


Microsoft has released pre-release version for System.DirectoryServices. You can get it from NuGet package manager using this command:

Install-Package System.DirectoryServices -Version 4.5.0-preview1-25914-04

This is working fine for me till now.




回答2:


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.



来源:https://stackoverflow.com/questions/45573144/asp-net-core-and-on-premise-ad-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!