I want to use anonymous profiles to store and use profile data without authenticated. How to enable anonymous profiles is described on page
https://msdn.microsoft.com/en-us/library/ewfkf772(v=vs.100).aspx
Profiles can also work with anonymous users. Support for anonymous
profiles is not enabled by default, so you must explicitly enable it.
In addition, when you define profile properties in the Web.config
file, you must explicitly make them available individually for
anonymous users. Profile properties do not support anonymous access by
default because profiles may be designed to work with authenticated
users, and many properties are likely to pertain to personal
information that is not available for anonymous users.
But I'm not sure, that I can use profile data for anonymous users. It says, that by default I can't use profile data. But not by default, have I ability to use profile data for anonymous users and if "yes" then what should I do?
I confused...
Answer
Anonymous profiles are possible. They let us associate profile data (e.g. a postal code) with an anonymous user. Of course, the profile data is only relevant while ASP.NET can identify the anonymous user via a cookie (or a query string parameter) like this:
But I'm not sure, that I can use profile data for anonymous users.
We can. I've just tested this on my local machine with ASP.NET MVC.
...and if "yes" then what should I do?
First, add the following to the system.web
section of the web.config file. It tells ASP.NET to track anonymous users, add a single profile property, and configure the provider with our default DB connection (assuming we have a connection string named DefaultConnection
.)
<anonymousIdentification enabled="true" />
<profile defaultProvider="SqlProvider" >
<properties>
<add name="PostalCode"
type="System.String"
allowAnonymous="true" />
</properties>
<providers>
<clear />
<add name="SqlProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="DefaultConnection" />
</providers>
</profile>
Once we've done that, ASP.NET will allow us to get/set profile properties as follows:
// get
var postalCode = Profile.GetPropertyValue("PostalCode") as string;
// set
Profile.SetPropertyValue("PostalCode", "V8K 1B1");
Profile.Save();
Note: you must setup Profiles on your database for this to work (otherwise ASP.NET will complain that it cannot find stored procedures.) One way to setup Profiles is to run aspnet_regsql
on your database like this.
aspnet_regsql -S "myDbServer" -A p -d "myDbName" -E
For more info about the aspnet_regsql
flags, run aspnet_regsql -?
for documentation. The -A p
sets up Profiles and the -E
uses integrated security.
See also:
For other approaches and more details, here are some other links:
- Walkthrough: Maintaining Web Site User Information with Profile Properties.
- How to assign Profile values?
- anonymousIdentification Element