HttpContext.Current.Profile.IsAnonymous == true, but ASPXAUTH cookie set
I'm using the ASP.NET membership and roles provider wrapper in 4.1. I'm able to log in, but HttpContext.Current.Profile.IsAnonymous is true while decrypting the authorization cookie shows the user is logged in (FormsAuthentication.Decrypt(Request.Cookies[".ASPXAUTH"].Value).Name).
I wanted to get the username of the current user, which I can get through the cookie instead of via HttpContext.Current.Profile.Username, but I figure if the profile isn't getting set, I'm doing something wrong and I should fix it.
Edit:
I suspect I need the profile to be populated to restrict which roles have access to which pages. I'm logged in with the right role, but I keep getting "System.UnauthorizedAccessException: You are not authorized to 'View a page' ('Pages')."
Thanks
I looked at HttpContext.Current.User (which from my understanding, HttpContext.Current.Profile is dynamically created based on HttpContext.Current.User).
Even though the Forms Authentication Cookie is set, the HttpContext.Current.User is still not authenticated and is anonymous. I was surprised the authentication type was Sitefinity.
HttpContext.Current.User
Id: 00000000-0000-0000-0000-000000000000
Provider:
Ticket:
AuthenticationType: Sitefinity
IsAuthenticated: False
Name: Anonymous
I've added the profile to the web.config
<
profile
enabled
=
"true"
defaultProvider
=
"MyProfileProvider"
>
<
providers>
<
add
name
=
"MyProfileProvider"
type
=
"System.Web.Profile.SqlProfileProvider"
connectionStringName
=
"MyDB"
applicationName
=
"/"
/>
</
providers
>
<
properties
>
<
add
allowAnonymous
=
"false"
name
=
"PersonId"
type
=
"int"
/>
</
properties
>
</
profile
>
<
userProfilesConfig
xmlns:config
=
"urn:telerik:sitefinity:configuration"
xmlns:type
=
"urn:telerik:sitefinity:configuration:type"
config:version
=
"4.1.1339.0"
>
<
providers
>
<
add
version
=
"4.1.1339.0"
name
=
"OpenAccessProfileProvider"
/>
</
providers
>
</
userProfilesConfig
>
Hi Eric,
We don't have Profile provider wrappers yet. In our Membership wrapper's ValidateUser() method I call the wrapped ASP.NET membership provider ValidateUser() - I guess this is enough for the Forms authentication cookie to be issued but there is also this method
FormsAuthentication.SetAuthCookie(userName, true);
I'm just not sure if both cookies won't interfere with each other. I will have to investigate and get back to you on this matter.
guys,
i am am not getting the FirstName when I use the following
var manager = UserManager.GetManager();
var currentUser = manager.GetUser(SecurityManager.GetCurrentUser().Identity.Name);
command.Parameters.Add(
new SqlParameter("@firstname", SqlDbType.NVarChar, 50)).Value = currentUser.FirstName;
command.Parameters.Add(
new SqlParameter("@lastname", SqlDbType.NVarChar, 50)).Value = currentUser.LastName;
FirstName goes to the database as null. Why?
Thanks,
Andrei
Hi Andrei,
Could you try to use the code below to get the FirstName
var profileTypeName = UserProfilesHelper.GetProfileTypeName(
typeof
(SitefinityProfile));
var upm = UserProfileManager.GetManager();
// pass the current user id - userId
var profile = upm.GetUserProfile(userId, profileTypeName) asSitefinityProfile;
if
(profile !=
null
)
var fn = profile.FirstName;
Ivan,
Again, thanks for the code. It worked. Sorry for posting it here too as well as in the ticket.
It was a bit of an emergency.
Thanks,
Andrei