26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
65 lines
2.3 KiB
C#
Executable File
65 lines
2.3 KiB
C#
Executable File
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Web.Mvc;
|
|
using DataModel.Models;
|
|
using WebInterface.Security;
|
|
|
|
namespace WebInterface.Helpers
|
|
{
|
|
/// <summary>
|
|
/// HTML helper methods
|
|
/// </summary>
|
|
public static class HtmlHelpers
|
|
{
|
|
/// <summary>
|
|
/// Gets current user for request
|
|
/// </summary>
|
|
/// <param name="html">HTML helper context</param>
|
|
/// <returns>LDAP entry for current session</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates logon control for user/session
|
|
/// </summary>
|
|
/// <param name="html">HTML helper context</param>
|
|
/// <returns>Raw HTML output for logon control</returns>
|
|
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("<a href='{0}' class='btn btn-primary'>Login</a>", urlHelper.Action("Login", "Account", null, null));
|
|
}
|
|
else
|
|
{
|
|
builder.AppendFormat("<span style='color: #9d9d9d;'>{0}</span> <a href='{1}' class='btn btn-primary'>Logout</a>", currentUser.DisplayName, urlHelper.Action("Logout", "Account", null, null));
|
|
}
|
|
|
|
return new MvcHtmlString(builder.ToString());
|
|
}
|
|
}
|
|
} |