03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Underst<strong>and</strong><strong>in</strong>g Profile x 645<br />

In the follow<strong>in</strong>g section, you see how to def<strong>in</strong>e profile properties <strong>in</strong> Web.config <strong>and</strong> how to access<br />

them <strong>in</strong> your web pages.<br />

NOTE It’s important to realize that the built-<strong>in</strong> Profile feature works only with<br />

Web Site Projects <strong>and</strong> not with Web Application Projects. For a discussion on<br />

the difference between the two, refer to Chapter 2. If you fi nd that none of the<br />

examples <strong>in</strong> this chapter seem to work, check that you haven’t accidentally created<br />

a Web Application Project. The simplest way to check is to look at the Code<br />

Beh<strong>in</strong>d file of a Web Form. If you see two Code Beh<strong>in</strong>d fi les (one named after the<br />

page with a .cs or .vb extension <strong>and</strong> one with an additional Designer extension),<br />

you have created a Web Application Project. In that case, get yourself<br />

a copy of the Chapter 16 folder that is part of the source that comes with this<br />

book <strong>and</strong> use that as the start<strong>in</strong>g po<strong>in</strong>t for this chapter.<br />

Configur<strong>in</strong>g the Profile<br />

You def<strong>in</strong>e a profile for your website <strong>in</strong> the Web.config file by creat<strong>in</strong>g a element as<br />

a direct child of the element. Between the tags, you need to create a<br />

element that is used to def<strong>in</strong>e the properties you want to expose from your Profile<br />

object. Two types of properties exist: simple properties <strong>and</strong> complex properties, referred to as profile<br />

groups.<br />

Creat<strong>in</strong>g Simple Profile Properties<br />

You def<strong>in</strong>e simple properties as direct children of the element us<strong>in</strong>g an element.<br />

The follow<strong>in</strong>g example demonstrates how to create a property that can be used to hold a<br />

user’s first name <strong>and</strong> one to hold a date of birth. The FirstName property can be accessed <strong>and</strong> set<br />

for authenticated <strong>and</strong> anonymous users, whereas the DateOfBirth property is accessible only to<br />

logged-<strong>in</strong> users:<br />

<br />

...<br />

<br />

<br />

<br />

<br />

<br />

<br />

Because properties are by default of type System.Str<strong>in</strong>g, there’s no need to def<strong>in</strong>e an explicit<br />

type on the property for text-based properties like a first name. However, for other types like<br />

a DateTime, a Boolean, an Integer, or your own types, you need to def<strong>in</strong>e the type explicitly<br />

us<strong>in</strong>g the type attribute <strong>and</strong> its fully qualified name <strong>in</strong>clud<strong>in</strong>g its namespace, as shown for the<br />

DateOfBirth property. The follow<strong>in</strong>g table lists the most common attributes of the element<br />

that <strong>in</strong>fluence the properties of a profile.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!