我怎么能反对南希的Active Directory进行身份验证?(How can I authent

2019-07-31 13:27发布

这是一个过时的文章,但http://msdn.microsoft.com/en-us/library/ff650308.aspx#paght000026_step3说明了什么我想做的事情。 我选择了南希的,因为它的简单性和低仪式方法我的网络框架。 所以,我需要一种方法来对使用Active Directory进行身份验证南希 。

在ASP.NET中,它看起来像您可以基于DB-成员提供与Active Directory之间只是在web.config文件中的一些设置只需切换。 我不需要那个特别,但有能力开发和生产之间的切换将是惊人的。

如何才能做到这一点?

Answer 1:

真正解决的办法是简单得多比它可能看起来。 试想活动目录为用户(就像一个数据库)的存储库。 所有你需要做的就是查询AD验证用户名和输入的密码是否有效。 所以,仅仅用南希的表单验证和处理引黄到AD在您的实现IUserMapper的。 以下是我想出了我的用户映射器:

public class ActiveDirectoryUserMapper : IUserMapper, IUserLoginManager
{
    static readonly Dictionary<Guid, long> LoggedInUserIds = new Dictionary<Guid, long>(); 

    readonly IAdminUserValidator _adminUserValidator;
    readonly IAdminUserFetcher _adminUserFetcher;
    readonly ISessionContainer _sessionContainer;

    public ActiveDirectoryUserMapper(IAdminUserValidator adminUserValidator, IAdminUserFetcher adminUserFetcher, ISessionContainer sessionContainer)
    {
        _adminUserValidator = adminUserValidator;
        _adminUserFetcher = adminUserFetcher;
        _sessionContainer = sessionContainer;
    }

    public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
    {
        _sessionContainer.OpenSession();
        var adminUserId = LoggedInUserIds.First(x => x.Key == identifier).Value;
        var adminUser = _adminUserFetcher.GetAdminUser(adminUserId);
        return new ApiUserIdentity(adminUser);
    }

    public Guid Login(string username, string clearTextPassword, string domain)
    {
        var adminUser = _adminUserValidator.ValidateAndReturnAdminUser(username, clearTextPassword, domain);
        var identifier = Guid.NewGuid();
        LoggedInUserIds.Add(identifier, adminUser.Id);
        return identifier;
    }
}

我保持我的数据库来处理角色的记录,所以这个类处理与AD验证和获取来自数据库的用户:

public class AdminUserValidator : IAdminUserValidator
{
    readonly IActiveDirectoryUserValidator _activeDirectoryUserValidator;
    readonly IAdminUserFetcher _adminUserFetcher;

    public AdminUserValidator(IAdminUserFetcher adminUserFetcher,
                              IActiveDirectoryUserValidator activeDirectoryUserValidator)
    {
        _adminUserFetcher = adminUserFetcher;
        _activeDirectoryUserValidator = activeDirectoryUserValidator;
    }

    #region IAdminUserValidator Members

    public AdminUser ValidateAndReturnAdminUser(string username, string clearTextPassword, string domain)
    {
        _activeDirectoryUserValidator.Validate(username, clearTextPassword, domain);

        return _adminUserFetcher.GetAdminUser(1);            
    }

    #endregion
}

而这一类实际上验证用户名/密码组合在Active Directory中:

public class ActiveDirectoryUserValidator : IActiveDirectoryUserValidator
{
    public void Validate(string username, string clearTextPassword, string domain)
    {
        using (var principalContext = new PrincipalContext(ContextType.Domain, domain))
        {
            // validate the credentials
            bool isValid = principalContext.ValidateCredentials(username, clearTextPassword);
            if (!isValid)
                throw new Exception("Invalid username or password.");
        }

    }
}


文章来源: How can I authenticate against Active Directory in Nancy?