EDN Admin
Well-known member
Hi,
I have successfully setup a custom membership provider that write to my own SQL database and would now like to store additional profile data in the same database. I have the standard asp.net 4.0 createuserwizard controls on my registration page.
To this end i have.
Create a custom profile provider that inherits from Profile Provider.
Amended the web.config
Created a set of stored procedures
When a user registers, my tblMember (custom membership) is populated as before but the Profiles table for my custom profile never gets populated.
Im assuming this should happen automatically once a user is authenticated and no page behind code is need, to initially create the profile record, as this is wired up in the web.config file.
Ive also tried placing a breakpoint in the custom profile class on method GetPropertyValues and SetPropertyValues but these never get hit during/after registration.
They do get hit if i put code in the Registration.aspx page in CreateUserWizard1_CreatedUser - such as
ProfileCommon p = Profile.GetProfile(mycustomember.UserName); - although they arent qute working correctly yet, the code does get callled.
Should i be using the code or similar above to create the profile record or should this be happening automatically - my membership record is created automically without any code behind, so im assuming the profile would follow the same route - although i
know i will have to set the additional profile properties in the code behind once the profile has been automatically created.
Also my database isnt in the app_data folder, as some example show.
Also i dont have all the aspnet....tables/spros that are needed when the out of the box profile is used - do i still need to have these present, even though im using a custom profile.
Code attached below (custom profile and web.config)
namespace HDI.AspNet.Membership<br/>
{<br/>
public sealed class HDIMembershipProfile : ProfileProvider<br/>
<br/>
{<br/>
<br/>
#region "class variables"<br/>
private string _applicationName;<br/>
private string _connectionString;<br/>
#endregion<br/>
<br/>
<br/>
public override void Initialize(string name, NameValueCollection config)<br/>
{<br/>
<br/>
_connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PP_Connect"].ConnectionString;<br/>
_applicationName = "/";<br/>
<br/>
// Get values from config file<br/>
if (config == null)<br/>
{<br/>
throw new ArgumentNullException("config");<br/>
}<br/>
<br/>
<br/>
if (name == null || name.Length == 0)<br/>
{<br/>
name = "HDIProfileProvider";<br/>
}<br/>
<br/>
<br/>
if (string.IsNullOrEmpty("description"))<br/>
{<br/>
config.Remove("description");<br/>
}<br/>
<br/>
//Set application and connecetionstring<br/>
<br/>
<br/>
base.Initialize(name, config);<br/>
}<br/>
<br/>
public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override int DeleteProfiles(string[] usernames)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override int DeleteProfiles(ProfileInfoCollection profiles)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override string ApplicationName<br/>
{<br/>
get<br/>
{<br/>
return _applicationName;<br/>
}<br/>
set<br/>
{<br/>
_applicationName = value;<br/>
}<br/>
}<br/>
<br/>
<br/>
<br/>
/// <summary><br/>
/// Get property values <br/>
/// </summary><br/>
/// <param name="context </param><br/>
/// <param name="settingsProperties </param><br/>
/// <returns></returns><br/>
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection settingsProperties)<br/>
{<br/>
string _userName = (string)context["UserName"];<br/>
bool _isAuthenticated = (bool)context["IsAuthenticated"];<br/>
<br/>
SqlConnection _sqlConnection = new SqlConnection(_connectionString);<br/>
SqlCommand _sqlCommand = new SqlCommand("Profile_Sel", _sqlConnection);<br/>
_sqlCommand.CommandType = CommandType.StoredProcedure;<br/>
SqlDataReader _sqlDataReader;<br/>
<br/>
_sqlCommand.Parameters.Add("username", SqlDbType.VarChar, 50).Value = _userName;<br/>
_sqlCommand.Parameters.Add("applicationName", SqlDbType.NVarChar, 50).Value = _applicationName;<br/>
_sqlCommand.Parameters.Add("isAnonymous", SqlDbType.Bit).Value = !_isAuthenticated;<br/>
<br/>
SettingsPropertyValueCollection _settingsValues = new SettingsPropertyValueCollection();<br/>
<br/>
try<br/>
{<br/>
_sqlConnection.Open();<br/>
_sqlDataReader = _sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);<br/>
_sqlDataReader.Read();<br/>
<br/>
foreach (SettingsProperty _property in settingsProperties)<br/>
{<br/>
// Check reader for data<br/>
if (_sqlDataReader.HasRows)<br/>
{<br/>
SettingsPropertyValue _settingsPropertyValue = new SettingsPropertyValue(_property);<br/>
<br/>
switch (_property.Name)<br/>
{<br/>
case "UserName":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["UserName"];<br/>
break;<br/>
<br/>
case "MemberTitleId":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["MemberTitleId"];<br/>
break;<br/>
<br/>
case "GenderId":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["GenderId"];<br/>
break;<br/>
<br/>
case "DateOfBirth":<br/>
break;<br/>
<br/>
case "FirstName":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["FirstName"];<br/>
break;<br/>
<br/>
case "LastName":<br/>
break;<br/>
<br/>
case "TownId":<br/>
break;<br/>
<br/>
case "Latitiude":<br/>
<br/>
break;<br/>
case "Longitude":<br/>
<br/>
break;<br/>
<br/>
default:<br/>
throw new Exception("unsupported property");<br/>
}<br/>
<br/>
_settingsValues.Add(_settingsPropertyValue);<br/>
}<br/>
<br/>
}<br/>
}<br/>
<br/>
catch (SqlException e)<br/>
{<br/>
}<br/>
<br/>
return _settingsValues;<br/>
<br/>
}<br/>
<br/>
<br/>
/// <summary><br/>
/// Set property values<br/>
/// </summary><br/>
/// <param name="context </param><br/>
/// <param name="settingsPropertyValues </param><br/>
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection settingsPropertyValues)<br/>
{<br/>
string _userName = (string)context["UserName"];<br/>
bool _isAuthenticated = (bool)context["IsAuthenticated"];<br/>
<br/>
SqlConnection _sqlConnection = new SqlConnection(_connectionString);<br/>
SqlCommand _sqlCommand = new SqlCommand("ProfileData_Upd", _sqlConnection);<br/>
_sqlCommand.CommandType = CommandType.StoredProcedure;<br/>
<br/>
_sqlCommand.Parameters.Add("userName", SqlDbType.NVarChar, 50).Value = _userName;<br/>
_sqlCommand.Parameters.Add("applicationName", SqlDbType.NVarChar, 50).Value = _applicationName;<br/>
_sqlCommand.Parameters.Add("isAnonymous", SqlDbType.Bit).Value = !_isAuthenticated;<br/>
<br/>
<br/>
<br/>
foreach (SettingsProperty settingsPropertyValue in settingsPropertyValues)<br/>
{<br/>
<br/>
switch (settingsPropertyValue.Name)<br/>
{<br/>
<br/>
case "MemberTitleId":<br/>
_sqlCommand.Parameters.Add("MemberTitleId", SqlDbType.Int, 0).Value = int.Parse (settingsPropertyValue.ToString()) ;<br/>
<br/>
break;<br/>
<br/>
case "GenderId":<br/>
_sqlCommand.Parameters.Add("GenderId", SqlDbType.Int, 0).Value = int.Parse(settingsPropertyValue.ToString());<br/>
break;<br/>
<br/>
case "DateOfBirth":<br/>
_sqlCommand.Parameters.Add("DateOfBirth", SqlDbType.DateTime, 0).Value = DateTime.Parse(settingsPropertyValue.ToString());<br/>
break;<br/>
<br/>
case "FirstName":<br/>
_sqlCommand.Parameters.Add("FirstName", SqlDbType.VarChar, 50).Value = settingsPropertyValue.ToString();<br/>
break;<br/>
<br/>
case "LastName":<br/>
_sqlCommand.Parameters.Add("LastName", SqlDbType.VarChar, 50).Value = settingsPropertyValue.ToString();<br/>
break;<br/>
<br/>
case "TownId":<br/>
_sqlCommand.Parameters.Add("TownId", SqlDbType.Int, 0).Value = int.Parse(settingsPropertyValue.ToString());<br/>
break;<br/>
<br/>
case "Latitiude":<br/>
_sqlCommand.Parameters.Add("Latitude", SqlDbType.Real, 0).Value = (Single)settingsPropertyValue.DefaultValue;<br/>
break;<br/>
<br/>
case "Longitude":<br/>
_sqlCommand.Parameters.Add("Longitude", SqlDbType.Real, 0).Value = (Single)settingsPropertyValue.DefaultValue;<br/>
break;<br/>
<br/>
default:<br/>
throw new Exception("unsupported property");<br/>
}<br/>
}<br/>
<br/>
try<br/>
{<br/>
_sqlConnection.Open();<br/>
_sqlCommand.ExecuteNonQuery();<br/>
}<br/>
<br/>
catch (SqlException e)<br/>
{<br/>
throw new Exception(e.Message);<br/>
}<br/>
<br/>
finally<br/>
{<br/>
_sqlConnection.Close();<br/>
}<br/>
}<br/>
}<br/>
}
web.config
<profile enabled="true" defaultProvider ="HDIMembershipProfile <br/>
<providers><br/>
<clear /><br/>
<add name ="HDIMembershipProfile"<br/>
type ="HDI.AspNet.Membership.HDIMembershipProfile"<br/>
connectionStringName="PP_Connect"<br/>
application="/" /><br/>
<!--<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>--><br/>
</providers><br/>
<properties ><br/>
<add name="UserId" type="System.Guid"/><br/>
<add name="MemberTitleId" type="System.Int32"/><br/>
<add name="GenderId" type="System.Int32"/><br/>
<add name="DateOfBirth" type="System.DateTime"/><br/>
<add name="FirstName" type="System.String"/><br/>
<add name="LastName" type="System.String"/><br/>
<!--<add name="TownId" type="System.Int32"/><br/>
<add name="Latitude" type="System.Single"/><br/>
<add name="Longitude" type="System.Single"/>--><br/>
</properties>
</profile>
Any help greatly appreciated
Steve
View the full article
I have successfully setup a custom membership provider that write to my own SQL database and would now like to store additional profile data in the same database. I have the standard asp.net 4.0 createuserwizard controls on my registration page.
To this end i have.
Create a custom profile provider that inherits from Profile Provider.
Amended the web.config
Created a set of stored procedures
When a user registers, my tblMember (custom membership) is populated as before but the Profiles table for my custom profile never gets populated.
Im assuming this should happen automatically once a user is authenticated and no page behind code is need, to initially create the profile record, as this is wired up in the web.config file.
Ive also tried placing a breakpoint in the custom profile class on method GetPropertyValues and SetPropertyValues but these never get hit during/after registration.
They do get hit if i put code in the Registration.aspx page in CreateUserWizard1_CreatedUser - such as
ProfileCommon p = Profile.GetProfile(mycustomember.UserName); - although they arent qute working correctly yet, the code does get callled.
Should i be using the code or similar above to create the profile record or should this be happening automatically - my membership record is created automically without any code behind, so im assuming the profile would follow the same route - although i
know i will have to set the additional profile properties in the code behind once the profile has been automatically created.
Also my database isnt in the app_data folder, as some example show.
Also i dont have all the aspnet....tables/spros that are needed when the out of the box profile is used - do i still need to have these present, even though im using a custom profile.
Code attached below (custom profile and web.config)
namespace HDI.AspNet.Membership<br/>
{<br/>
public sealed class HDIMembershipProfile : ProfileProvider<br/>
<br/>
{<br/>
<br/>
#region "class variables"<br/>
private string _applicationName;<br/>
private string _connectionString;<br/>
#endregion<br/>
<br/>
<br/>
public override void Initialize(string name, NameValueCollection config)<br/>
{<br/>
<br/>
_connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PP_Connect"].ConnectionString;<br/>
_applicationName = "/";<br/>
<br/>
// Get values from config file<br/>
if (config == null)<br/>
{<br/>
throw new ArgumentNullException("config");<br/>
}<br/>
<br/>
<br/>
if (name == null || name.Length == 0)<br/>
{<br/>
name = "HDIProfileProvider";<br/>
}<br/>
<br/>
<br/>
if (string.IsNullOrEmpty("description"))<br/>
{<br/>
config.Remove("description");<br/>
}<br/>
<br/>
//Set application and connecetionstring<br/>
<br/>
<br/>
base.Initialize(name, config);<br/>
}<br/>
<br/>
public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override int DeleteProfiles(string[] usernames)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override int DeleteProfiles(ProfileInfoCollection profiles)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)<br/>
{<br/>
throw new NotImplementedException();<br/>
}<br/>
<br/>
public override string ApplicationName<br/>
{<br/>
get<br/>
{<br/>
return _applicationName;<br/>
}<br/>
set<br/>
{<br/>
_applicationName = value;<br/>
}<br/>
}<br/>
<br/>
<br/>
<br/>
/// <summary><br/>
/// Get property values <br/>
/// </summary><br/>
/// <param name="context </param><br/>
/// <param name="settingsProperties </param><br/>
/// <returns></returns><br/>
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection settingsProperties)<br/>
{<br/>
string _userName = (string)context["UserName"];<br/>
bool _isAuthenticated = (bool)context["IsAuthenticated"];<br/>
<br/>
SqlConnection _sqlConnection = new SqlConnection(_connectionString);<br/>
SqlCommand _sqlCommand = new SqlCommand("Profile_Sel", _sqlConnection);<br/>
_sqlCommand.CommandType = CommandType.StoredProcedure;<br/>
SqlDataReader _sqlDataReader;<br/>
<br/>
_sqlCommand.Parameters.Add("username", SqlDbType.VarChar, 50).Value = _userName;<br/>
_sqlCommand.Parameters.Add("applicationName", SqlDbType.NVarChar, 50).Value = _applicationName;<br/>
_sqlCommand.Parameters.Add("isAnonymous", SqlDbType.Bit).Value = !_isAuthenticated;<br/>
<br/>
SettingsPropertyValueCollection _settingsValues = new SettingsPropertyValueCollection();<br/>
<br/>
try<br/>
{<br/>
_sqlConnection.Open();<br/>
_sqlDataReader = _sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);<br/>
_sqlDataReader.Read();<br/>
<br/>
foreach (SettingsProperty _property in settingsProperties)<br/>
{<br/>
// Check reader for data<br/>
if (_sqlDataReader.HasRows)<br/>
{<br/>
SettingsPropertyValue _settingsPropertyValue = new SettingsPropertyValue(_property);<br/>
<br/>
switch (_property.Name)<br/>
{<br/>
case "UserName":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["UserName"];<br/>
break;<br/>
<br/>
case "MemberTitleId":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["MemberTitleId"];<br/>
break;<br/>
<br/>
case "GenderId":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["GenderId"];<br/>
break;<br/>
<br/>
case "DateOfBirth":<br/>
break;<br/>
<br/>
case "FirstName":<br/>
_settingsPropertyValue.PropertyValue = _sqlDataReader["FirstName"];<br/>
break;<br/>
<br/>
case "LastName":<br/>
break;<br/>
<br/>
case "TownId":<br/>
break;<br/>
<br/>
case "Latitiude":<br/>
<br/>
break;<br/>
case "Longitude":<br/>
<br/>
break;<br/>
<br/>
default:<br/>
throw new Exception("unsupported property");<br/>
}<br/>
<br/>
_settingsValues.Add(_settingsPropertyValue);<br/>
}<br/>
<br/>
}<br/>
}<br/>
<br/>
catch (SqlException e)<br/>
{<br/>
}<br/>
<br/>
return _settingsValues;<br/>
<br/>
}<br/>
<br/>
<br/>
/// <summary><br/>
/// Set property values<br/>
/// </summary><br/>
/// <param name="context </param><br/>
/// <param name="settingsPropertyValues </param><br/>
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection settingsPropertyValues)<br/>
{<br/>
string _userName = (string)context["UserName"];<br/>
bool _isAuthenticated = (bool)context["IsAuthenticated"];<br/>
<br/>
SqlConnection _sqlConnection = new SqlConnection(_connectionString);<br/>
SqlCommand _sqlCommand = new SqlCommand("ProfileData_Upd", _sqlConnection);<br/>
_sqlCommand.CommandType = CommandType.StoredProcedure;<br/>
<br/>
_sqlCommand.Parameters.Add("userName", SqlDbType.NVarChar, 50).Value = _userName;<br/>
_sqlCommand.Parameters.Add("applicationName", SqlDbType.NVarChar, 50).Value = _applicationName;<br/>
_sqlCommand.Parameters.Add("isAnonymous", SqlDbType.Bit).Value = !_isAuthenticated;<br/>
<br/>
<br/>
<br/>
foreach (SettingsProperty settingsPropertyValue in settingsPropertyValues)<br/>
{<br/>
<br/>
switch (settingsPropertyValue.Name)<br/>
{<br/>
<br/>
case "MemberTitleId":<br/>
_sqlCommand.Parameters.Add("MemberTitleId", SqlDbType.Int, 0).Value = int.Parse (settingsPropertyValue.ToString()) ;<br/>
<br/>
break;<br/>
<br/>
case "GenderId":<br/>
_sqlCommand.Parameters.Add("GenderId", SqlDbType.Int, 0).Value = int.Parse(settingsPropertyValue.ToString());<br/>
break;<br/>
<br/>
case "DateOfBirth":<br/>
_sqlCommand.Parameters.Add("DateOfBirth", SqlDbType.DateTime, 0).Value = DateTime.Parse(settingsPropertyValue.ToString());<br/>
break;<br/>
<br/>
case "FirstName":<br/>
_sqlCommand.Parameters.Add("FirstName", SqlDbType.VarChar, 50).Value = settingsPropertyValue.ToString();<br/>
break;<br/>
<br/>
case "LastName":<br/>
_sqlCommand.Parameters.Add("LastName", SqlDbType.VarChar, 50).Value = settingsPropertyValue.ToString();<br/>
break;<br/>
<br/>
case "TownId":<br/>
_sqlCommand.Parameters.Add("TownId", SqlDbType.Int, 0).Value = int.Parse(settingsPropertyValue.ToString());<br/>
break;<br/>
<br/>
case "Latitiude":<br/>
_sqlCommand.Parameters.Add("Latitude", SqlDbType.Real, 0).Value = (Single)settingsPropertyValue.DefaultValue;<br/>
break;<br/>
<br/>
case "Longitude":<br/>
_sqlCommand.Parameters.Add("Longitude", SqlDbType.Real, 0).Value = (Single)settingsPropertyValue.DefaultValue;<br/>
break;<br/>
<br/>
default:<br/>
throw new Exception("unsupported property");<br/>
}<br/>
}<br/>
<br/>
try<br/>
{<br/>
_sqlConnection.Open();<br/>
_sqlCommand.ExecuteNonQuery();<br/>
}<br/>
<br/>
catch (SqlException e)<br/>
{<br/>
throw new Exception(e.Message);<br/>
}<br/>
<br/>
finally<br/>
{<br/>
_sqlConnection.Close();<br/>
}<br/>
}<br/>
}<br/>
}
web.config
<profile enabled="true" defaultProvider ="HDIMembershipProfile <br/>
<providers><br/>
<clear /><br/>
<add name ="HDIMembershipProfile"<br/>
type ="HDI.AspNet.Membership.HDIMembershipProfile"<br/>
connectionStringName="PP_Connect"<br/>
application="/" /><br/>
<!--<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>--><br/>
</providers><br/>
<properties ><br/>
<add name="UserId" type="System.Guid"/><br/>
<add name="MemberTitleId" type="System.Int32"/><br/>
<add name="GenderId" type="System.Int32"/><br/>
<add name="DateOfBirth" type="System.DateTime"/><br/>
<add name="FirstName" type="System.String"/><br/>
<add name="LastName" type="System.String"/><br/>
<!--<add name="TownId" type="System.Int32"/><br/>
<add name="Latitude" type="System.Single"/><br/>
<add name="Longitude" type="System.Single"/>--><br/>
</properties>
</profile>
Any help greatly appreciated
Steve
View the full article