I’m working with ASP.Net Membership with FormsAuth and have run into a strange problem. Here’s the code where I set my Auth cookie:
FormsAuthentication.SetAuthCookie(emailAddress, true);
string returnUrl = Session["returnUrl"] + "";
The returnUrl variable is passed to a meta refresh tag on the page’s HTML, so the redirect is client side. I did this to make sure the auth cookie was set in the browser before redirecting.
The returnUrl is a page I setup for debugging purposes. It has the following code in its ASPX
<%
var user = System.Web.Security.Membership.GetUser();
%>
<%
if (user != null)
{%>
Name: <%=user.UserName%>
<%}
else
{%>
No user found in session
<%} %>
<br />
User.Identity.IsAuthenticated: <%=User.Identity.IsAuthenticated%><br />
User.Identity.Name: <%=User.Identity.Name%><br />
When I visit the page, IsAuthenticated is true, but the user and the Indentity.Name values are null. I authenticated and set the auth cookie; so why isn’t the user info available?
Any constructive input is greatly appreciated.
Turns out this was total PEBKAC on my part. What I didn’t notice was that the emailAddress string was null; and had set the auth cookie with the username null; causing the issues described.