using System.Security.Claims; using System.Text; using System.Web.Mvc; using DataModel.Models; using WebInterface.Security; namespace WebInterface.Helpers { /// /// HTML helper methods /// public static class HtmlHelpers { /// /// Gets current user for request /// /// HTML helper context /// LDAP entry for current session public static LDAPEntry CurrentUser(this HtmlHelper html) { //Verify user is authenticated before continuing if (!html.ViewContext.RequestContext.HttpContext.User.Identity.IsAuthenticated) { return null; } LDAPEntry currentUser = null; try { UserIdentity identity = new UserIdentity(html.ViewContext.RequestContext.HttpContext.User.Identity as ClaimsIdentity); currentUser = identity.ToLDAPEntry(); } catch { //Do nothing } return currentUser; } /// /// Generates logon control for user/session /// /// HTML helper context /// Raw HTML output for logon control public static MvcHtmlString LogonControl(this HtmlHelper html) { LDAPEntry currentUser = html.CurrentUser(); UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext); StringBuilder builder = new StringBuilder(); if (currentUser == null || html.ViewContext.RequestContext.HttpContext.User.Identity==null|| !html.ViewContext.RequestContext.HttpContext.User.Identity.IsAuthenticated) { builder.AppendFormat("Login", urlHelper.Action("Login", "Account", null, null)); } else { builder.AppendFormat("{0} Logout", currentUser.DisplayName, urlHelper.Action("Logout", "Account", null, null)); } return new MvcHtmlString(builder.ToString()); } } }