Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the authentication I use with most of my ASP.NET / Mvc applications that needs basic authentication. I believe it would help and would allow you to store additional data in the AuthenticationCookie. If you use this before .Net does their authentication .Net will override your setting of the current principal and user properties, using the PostAuthenticateRequest will fix the overriding issue. Let me know if you have any questions.</p> <pre><code> // Code that goes in the Global.asax.cs // that runs after .Net has done it's authentication protected void Application_PostAuthenticateRequest(object sender, EventArgs e) { IPrincipal user = HttpContext.Current.User; if (user == null) { return; } FormsIdentity formsIdentity = user.Identity as FormsIdentity; if (formsIdentity == null || !formsIdentity.IsAuthenticated) { return; } Principal principal = new Principal(new Identity(formsIdentity.Ticket)); HttpContext.Current.User = principal; Thread.CurrentPrincipal = principal; } </code></pre> <pre><code> // Base implementation of the System.Security.Principal.IPrincipal interface. public class Principal : System.Security.Principal.IPrincipal { #region Fields private IIdentity _identity = null; #endregion #region Constructors public Principal(IIdentity identity) { _identity = identity; } #endregion #region Properties public IIdentity Identity { get { return _identity; } } #endregion #region Methods public bool IsInRole(string role) { return (_identity != null && _identity.IsAuthenticated); } #endregion } // Base implementation of the System.Security.Principal.IIdentity interface. public class Identity : System.Security.Principal.IIdentity { #region Fields private readonly int _userId; private readonly bool _isAuthenticated; private readonly string _userName; #endregion #region Constructors public Identity(FormsAuthenticationTicket formsAuthTicket) { if (formsAuthTicket == null) { throw new NullReferenceException("FormsAuthenticationTicket may not be null."); } if (string.IsNullOrEmpty(formsAuthTicket.UserData)) { throw new NullReferenceException("FormsAuthenticationTicket.UserData may not be null or empty."); } string[] userData = formsAuthTicket.UserData.Split(new[] {"|"}, StringSplitOptions.RemoveEmptyEntries); if (userData.Length &lt; 1) { throw new ArgumentOutOfRangeException("formsAuthTicket", userData, "UserData does not contain a UserId and or a SiteId"); } _userId = Convert.ToInt32(userData[0]); _isAuthenticated = !formsAuthTicket.Expired; _userName = formsAuthTicket.Name; } #endregion #region Properties public int UserId { get { return _userId; } } public string AuthenticationType { get { return "Forms"; } } public bool IsAuthenticated { get { return _isAuthenticated; } } public string Name { get { return _userName; } } #endregion } </code></pre> <p>I Hope this helps to solve your issue.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload