Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Authentication Cookie Not Being Retrieved
    primarykey
    data
    text
    <p>I am having a hard time implementing "Remember Me" functionality in an MVC application with a custom principal. I have boiled it down to ASP.NET not retrieving the authentication cookie for me. I have included a snapshot below from Google Chrome.</p> <ol> <li><p>Shows the results of Request.Cookies that is set within the controller action and placed in ViewData for the view to read. Notice that it is missing the .ASPXAUTH cookie</p></li> <li><p>Shows the results from the Chrome developer tools. You can see that .ASPXAUTH is included here.</p></li> </ol> <p><a href="http://i50.tinypic.com/ibctjd.png" rel="nofollow noreferrer">alt text http://i50.tinypic.com/ibctjd.png</a></p> <p>What may be the issue here? Why does ASP.NET not read this value from the cookie collection?</p> <p>My application uses a custom IPrincipal. BusinessPrincipalBase is a CSLA object that ust implements IPrincipal. Here is the code for that:</p> <pre><code>[Serializable()] public class MoralePrincipal : BusinessPrincipalBase { private User _user; public User User { get { return _user; } } private MoralePrincipal(IIdentity identity) : base(identity) { if (identity is User) { _user = (User)identity; } } public override bool Equals(object obj) { MoralePrincipal principal = obj as MoralePrincipal; if (principal != null) { if (principal.Identity is User &amp;&amp; this.Identity is User) { return ((User)principal.Identity).Equals(((User)this.Identity)); } } return base.Equals(obj); } public override int GetHashCode() { return base.GetHashCode(); } public static bool Login(string username, string password) { User identity = User.Fetch(username, password); if (identity == null || !identity.IsAuthenticated) { identity = (User)User.UnauthenicatedIdentity; } MoralePrincipal principal = new MoralePrincipal(identity); Csla.ApplicationContext.User = principal; Context.Current.User = identity; return identity != null &amp;&amp; identity.IsAuthenticated; } public static void Logout() { IIdentity identity = User.UnauthenicatedIdentity; MoralePrincipal principal = new MoralePrincipal(identity); ApplicationContext.User = principal; Context.Current.User = identity as User; } public override bool IsInRole(string role) { if (Context.Current.User == null || Context.Current.Project == null) { return false; } string userRole = Context.Current.User.GetRole(Context.Current.Project.Id); return string.Compare(role, userRole, true) == 0; } </code></pre> <p>The application also uses a custom membership provider. Here is the code for that.</p> <pre><code>public class MoraleMembershipProvider : MembershipProvider { public override bool ValidateUser(string username, string password) { bool result = MoralePrincipal.Login(username, password); HttpContext.Current.Session["CslaPrincipal"] = ApplicationContext.User; return result; } #region Non-Implemented Properties/Methods public override string ApplicationName { get { return "Morale"; } set { throw new NotImplementedException(); } } // Everything else just throws a NotImplementedException #endregion } </code></pre> <p>I do not think that any of this is related because the bottom line is that the Request.Cookies does not return the authentication cookie. Is it related to the size of the cookie? I heard there are issues to the size of the cookie.</p> <p>UPDATE: It seems that the issue revolves around subdomains. This site was being hosted with a subdomain and the cookie domain was left blank. Does anyone have any pointers on how I can get the auth cookie to work with all domains (e.g. <a href="http://example.com" rel="nofollow noreferrer">http://example.com</a>, <a href="http://www.example.com" rel="nofollow noreferrer">http://www.example.com</a>, and <a href="http://sub.example.com" rel="nofollow noreferrer">http://sub.example.com</a>)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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