Using the Profile Object Inside a Web Service ASP.NET

The web project uses a Profile object like Is FirstVisit to store a user ’s state.Profile object is also used to get the currently visiting user’s name from the Profile.User Name property.The Default.aspx is already full of Profile. Something, and so are the wid get container and the wid gets.The next step is to add a new web service and access the Profile object, but when you type context.Profile from the web service code,IntelliSense doesn’t show any of your custom properties.

At run time, ASP.NET generates a class looking at the specification provided in web.config,which becomes the Profile object in.aspx pages and .ascx controls. But this object is not available in the web service(.asmx.cs) and you cannot see the custom properties you have added in the Profile object. Although HttpContext.Current.Profile will reference the Profile object,its type is Profile Base, which does not show your custom properties because the class is generated at runtime.

To overcome this problem, you have to handcode the profile class in your App_Code folder and then configure web.config so it doesn’t autogenerate a class but instead uses yours. Here’s what you do in web.config:

<profile enabled="true" inherits="UserProfile">

Now go to App_Code and make a UserProfile class like this:

public class UserProfile : System .Web .Profile.ProfileBase

This is an example property implementation.Don’t forget to add the [Settings Allow Anonymous Attribute(true)] to the properties that you want to make available to anonymous users. At the end of the class, add this method:

public virtual ProfileCommon GetProfile(string username)

Here’s an easy way to avoid hand coding this class and generate it automatically instead. Before you make the changes in web.config and create the UserProfile class, run your web project as it was before. But before running it, turn off SQL Server.This will make the ASP.NET execution break on the first call to a Profile object’s property. For example,if you have defined a custom property TimeZone in the Profile object in web.config, execution will break on this line:

public virtual int Timezone

It will fail to load the Profile object values from the database because the data base is down. If you scroll up, you will see that this is the class that ASP.NET generates at runtime. All the properties are already declared on this class.So, you can just copy and paste it in your own class easily! However,after copying, you will realize there’s no [Settings Allow Anonymous Attribute(true)] attribute in the generated class. So, you will have to add it manually.Once you have made the class, you will have to remove all the custom properties declared inside <properties> node in the web.config.

Now that you have your own Profile class, you can cast (Http Context . Current.Profile as UserProfile) and use all the custom properties inside the web service.

If you don’t want to have strict coding on the web service, then you can use the old way of accessing the Profile properties via Profile.Get Property Value(" Time Zone").This will work for both web pages and web services .You don’t need to handcode a Profile class if you go for this approach, then again you don’t get the strong typing and the IntelliSense feature.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

ASP.NET Topics