0

I have to implement an interface like this:

interface IMembershipWrapper
{
  Guid GetUserId();
  Guid GetUserId(string username, bool userIsOnline);
  bool ValidateUser(string userName, string password);
    …
}

against active directory and inject it using Unity.

I might get away with throwing a NotImplementedException exception for certain methods but do you think it is generally possible? What strategy do you recommend?

I understand that I can configure 'active directory asp.net forms authentication' via the web.config as described here. Unfortunately, this is not an option.

1
  • Cam you use Windows authentication? Commented Mar 22, 2013 at 15:53

1 Answer 1

3

This should be completely possible without changing your authentication system in the web.config. Especially if you're using .NET 3.5+. Take a look at System.DirectoryServices.AccountManagement.

To implement GetUserId(string username, bool userIsOnline) you may want to try something like:

public Guid GetUserId(string username, bool userIsOnline) {
    using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "[active directory domain here]")) {
        var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username);
        if(user != null)
            return user.Guid.Value;
        else
            return null;
    }
}

To implement ValidateUser(string userName, string password) use ValidateCredentials() on the PrinicalContext

public bool ValidateUser(string userName, string password) {
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "[active directory domain here]"))
    {
        return pc.ValidateCredentials(userName, password);
    }
}

Without more information about your implementation, I'm not sure how to go about implementing GetUserId(), since it seems like you wouldn't have enough information to go to Active Directory with.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.