I am designing a web application using the ASP.net MVC framework. I would like to use Windows Authentication and do Role Checks using the Role Manager SQLRoleProvider.
In MVC5 application give the action as given below
public ActionResult Index()
{
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
ViewBag.Name = name;
ViewBag.Email = uEmail(User.Identity.Name.Replace(@"YOURDOMAIN\", ""));
return View();
}
Change the YOURDOMAIN to your domain name. And give the uEmail function as given below. Also add the reference to using System.Security.Principal and using System.DirectoryServices.
private string uEmail(string uid)
{
DirectorySearcher dirSearcher = new DirectorySearcher();
DirectoryEntry entry = new DirectoryEntry(dirSearcher.SearchRoot.Path);
dirSearcher.Filter = "(&(objectClass=user)(objectcategory=person)(mail=" + uid + "*))";
SearchResult srEmail = dirSearcher.FindOne();
string propName = "mail";
ResultPropertyValueCollection valColl = srEmail.Properties[propName];
try
{
return valColl[0].ToString();
}
catch
{
return "";
}
}