Pregunta

I have the following code at some point in Application_Start

MembershipUserCollection users = Membership.GetAllUsers();

foreach (MembershipUser u in users)
{
    ProfileBase profile = ProfileBase.Create(u.UserName, false);

    if (profile["Index"] != null)
    {
        // Some code
    }
}

The problem is that when i try to access the profile properties I get "Request is not available in this context" httpException. I have tried using profile.GetPropertyValue but no luck also.

Why does profile need the request object and how can I get this to work?

EDIT:

I have looked into the .NET code and found that at some point SqlProfileProvider.GetPropertyValuesFromDatabase(string userName, SettingsPropertyValueCollection svc) is called, inside of which rests this string of code:

if (!current.Request.IsAuthenticated)

Apparently this will never work if not in request context. The workaround descripbed in Onur's answer will work fine.

¿Fue útil?

Solución

Apparently you are trying to access request object which is not possible in your case.

Please take a look following link and apply one of workarounds if possible. http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart

and if you give more information what are you trying to achieve we can find another way.

Otros consejos

You could get the "string value" properties by querying the aspnet_Profiles table and parsing the string.

SELECT [UserName]
  ,[PropertyNames]
  ,[PropertyValuesString]
FROM [aspnet_Profile]
LEFT JOIN [aspnet_Users] on [aspnet_Users].UserId = [aspnet_Profile].UserId

The PropertyValuesString can be parsed with a bit of code. This link should explain the format in detail https://msdn.microsoft.com/en-us/library/aa478953.aspx#persistence_format.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top