Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've been reading a lot of guides/articles but haven't found one yet that does exactly what I want... that is to implement Active Directory Authentication in an ASP.NET Web API through forms.

Something like on this guide:

Cool MVC 5 guide to implement authentication with Active Directory

Which is very good but it's for MVC, i.e., it uses a Controller not an ApiController

Can someone please give me hints/tips/articles on how to start? Especially about the part that connects to the active directory. I've been stuck on this for a while.

UPDATE:

C#
public bool IsAuthenticatedUser(string srvr, string usr, string password)
       {
           bool authenticated = false;

           try {
               DirectoryEntry entry = new DirectoryEntry(srvr, usr, password);
               object nativeObject = entry.NativeObject;
               Object obj = entry.NativeObject;
               authenticated = true;
           }
           catch {
               throw new HttpResponseException(HttpStatusCode.Unauthorized);
           }
           return authenticated;
       }

       // POST: api/Login
       public void Post([FromBody]string username, [FromBody]string password)
       {
           if (IsAuthenticatedUser("LDAP string", username, password))
           {
               Redirect("Index");
           }
           else
           {
               throw new HttpResponseException(HttpStatusCode.Unauthorized);
           }
       }


I was thinking of trying something like this for the authentication, your thoughts?
Posted
Updated 30-Mar-15 23:56pm
v3
Comments
F-ES Sitecore 27-Mar-15 8:03am    
A web API doesn't have forms, you might need to expand a bit on what you're looking to do. Also the article you posted explains how to connect to AD also, so what exactly are you having problems with as it seems you already have access to the solution?
varmartins 27-Mar-15 8:10am    
The problem is I want to call the api from the frontend which is being done with Ember.js so I would want for it to receive the data (username and password) check it against the active directory and return if the user exists and his group of permissions
F-ES Sitecore 27-Mar-15 8:59am    
Just do a normal form submission the way you would with any form, then on the click event of the login button use the "if (Membership.ValidateUser(model.UserName, model.Password))" code - except your username and password comes from txtUsername.Text and txtPassword.Text rather than model.UserName etc.
varmartins 27-Mar-15 11:03am    
And where would i save the response (authorization group) so that I could access from the frontend?

1 solution

// Add 3 references to your project
System.DirectoryService
System.DirectoryService.Protocols
System.DirectoryService.AccountManagement

// create a bool method

public bool IsAuthenticatedUser(string srvr, string usr, string password)
{
bool authenticated = false;
try
{
DirectoryEntry entry = new DirectoryEntry(srvr, usr, password);
object nativeObject = entry.NativeObject;
Object obj = entry.NativeObject;
authenticated = true;
}
catch
{
// LableError.Text = "Invalid UserName";
}
return authenticated;
}



// Then put this code inside the login button

protected void login_Click(object sender, EventArgs e)
{
if (IsAuthenticatedUser("", "YourADName\\" + UserNameText.Text,PasswordText.Text))
{
// Do something such as redirect somewhere
}
else
{

errorMessage.Text = "Invalid User";
}


}
 
Share this answer
 
Comments
varmartins 30-Mar-15 6:42am    
It is something like this that I want... but what I want is for the method to receive data through json (since I'm sending it from Ember.js) and to return the users permission group

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900