Pages

Showing posts with label Active Directory. Show all posts
Showing posts with label Active Directory. Show all posts

Monday, November 30, 2015

Azure Active Directory Authentication with roles

Wednesday, April 9, 2014

Azure Active Directory integration in asp.net MVC

Azure Active Directory integration in asp.net MVC

Pre- requisite:
1. Visual Studio 2012 Professional or Visual Studio 2012 Ultimate
2. Identity and Access Tools for Visual Studio 2012
3. Azure Subscription

Step 1:
 You need to create Active directory in your Azure subscription. You need to register your application in azure subscription.
 Active directory is called as tenant. Your users from your active directory(tenant) are able to login to your application.
 other users are not able to login. To make other users access available then you need to implement step 3 as well.
Details explained in following link.
 http://msdn.microsoft.com/en-us/library/windowsazure/dn151790.aspx

Step 2:
You can achieve following:
 1. Getting full user details
 2. Creating and Updating Users
 3. Getting a list of groups
 4. Updating group membership
Details explained in following link.
 http://msdn.microsoft.com/en-us/library/windowsazure/dn151791.aspx

Step 3:
 If you want to allow access to other user from your organization. then you need to implement multiple tenant(active directory) in your application.
 So other user will be able to login to your application.
Details explained in following link.
 http://msdn.microsoft.com/en-us/library/windowsazure/dn151789.aspx 

Monday, March 18, 2013

Get users information through active directory

Get users information through active directory. You need to add System.DirectoryServices dll reference in your asp.net project. Add following code in default.aspx page.
it automatically finds user name from Page.User.Identity.Name. We have added some properties like: sAMAccountName it only load those properties which we have mentioned in our code.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using System.Security.Authentication;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SearchResultCollection users;
        string loginUserName = "";
        if (string.IsNullOrEmpty(Page.User.Identity.Name) == false)
        {
            loginUserName = Page.User.Identity.Name.ToString().Substring(Page.User.Identity.Name.ToString().LastIndexOf("\\") + 1);
        }
        using (DirectoryEntry Root = new DirectoryEntry())
        {
            //Establish connection to current loged on users Active Directory
            using (DirectorySearcher Searcher = new DirectorySearcher(Root))
            {
                //Start at the top              
                Searcher.Filter = "(&(objectCategory=person)(anr=" + loginUserName.ToUpper().Trim() + "))";
                Searcher.SearchScope = SearchScope.Subtree;
                //Start at the top and keep drilling down
                Searcher.PropertiesToLoad.Add("sAMAccountName");
                //Load User ID
                Searcher.PropertiesToLoad.Add("displayName");
                //Load Display Name
                Searcher.PropertiesToLoad.Add("givenName");
                //Load Users first name
                Searcher.PropertiesToLoad.Add("sn");
                //Load Users last name
                Searcher.PropertiesToLoad.Add("distinguishedName");
                //Users Distinguished name
                Searcher.PropertiesToLoad.Add("telephoneNumber");
                //Ext. Number
                Searcher.PropertiesToLoad.Add("ipPhone");
                //7D Phone Number
                Searcher.PropertiesToLoad.Add("mobile");
                //Cell Phone Number
                Searcher.PropertiesToLoad.Add("mail");
                //Cell Phone Number
                Searcher.Sort.PropertyName = "sn";
                //Sort by last name
                Searcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
                //A-Z
                users = Searcher.FindAll();
                {
                    //Users contains our searh results
                    //MsgBox(users.Count)
                    //If it's zero then no matches were found
                    if (users.Count > 0)
                    {
                        //goes throug each user in the search results
                        foreach (SearchResult User in users)
                        {
                           // ResultPropertyCollection rcol = User.Properties;
                            ResultPropertyCollection props = User.Properties;
                            foreach (string prop in props.PropertyNames )
                            {
                                ResultPropertyValueCollection values = props[prop];
                                foreach (string val in values)
                                {
                                    //if (prop=="mail")
                                    //    Response.Write(val);
                                    Response.Write("<br>"+prop + "= " + val);
                                }
                            }  
                        }
                    }
                    else
                    {
                        // Store the DataTable in ViewState.
                        return;
                    }
                }
            }
        }
    }
}