Wednesday, September 23, 2015

MVC - Some key points for the developers.

Here I am trying to cover some basics key points for the MVC

Note:

1) Let say you have different- different css and that need to be applied on the body, How you will do from the view itself.

Solution :-            Add the dynamic css on _Layout like below and set this in the view section.                                                                                                                                                                                       <body class="@ViewBag.BodyClass"> 
From View write below code.

@{
    ViewBag.Title = "Home";
    //ViewBag.Title = "This page title!";
    ViewBag.BodyClass = "landing-page";
}
 -------------------------------------------------------------------------------------------------------------------

2) How to use multiple layouts in the MVC

Solution:- 
                                         
@{
    var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
    string cLayout = "";
    if (controller == "Index")
    {
        cLayout = "~/Views/Shared/_LayoutLanding.cshtml";
    }
    else
    {
        cLayout = "~/Views/Shared/_Layout.cshtml";
    }
    Layout = cLayout;
}

 
----------------------------------------------------------------------------------------------------------------------
3) How to implement LDAP authentication in MVC.

Solution:-  For implementation of LDAP we required the LDAP path but we can get this dynamic on the basis of domain and use it.

Namespaces required:-

using System.DirectoryServices.Protocols;
using System.DirectoryServices;
using System.Net;

Sample Code for getting default ldap path



String defaultNamingContext = string.Empty;
                      using (DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE"))
                      {
                          if (deRoot.Properties["defaultNamingContext"] != null)
                          {
                              defaultNamingContext =
                                     deRoot.Properties["defaultNamingContext"].Value.ToString();
                          }
                      }


Use the path to LDAP authentication


try
                  {
                      String defaultNamingContext = string.Empty;
                      using (DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE"))
                      {
                          if (deRoot.Properties["defaultNamingContext"] != null)
                          {
                              defaultNamingContext =
                                     deRoot.Properties["defaultNamingContext"].Value.ToString();
                          }
                      }

                      DirectoryEntry entry = new DirectoryEntry("LDAP://" + defaultNamingContext, "jitendra.so@hXX.com", "XX@!@#$%^");
                      object nativeObject = entry.NativeObject;
                     bool authenticated = true;
                  }
                  catch(Exception erx)
                  {

                      String msg = erx.Message;
                  }

-----------------------------------------------------------------------------------------------------------------