I am trying to add an example table profile provider from http://www.asp.net/downloads/sandbox/table-profile-provider-samples to a new MVC 2 site.
After doing a bit of research and messing around, I came to a profile class that looks like this.
namespace MyNamespace.Models
{
public class UserProfile : ProfileBase
{
[SettingsAllowAnonymous(false),CustomProviderData("FirstName;nvarchar")]
public string FirstName
{
get { return base["FirstName"] as string; }
set { base["FirstName"] = value; }
}
[SettingsAllowAnonymous(false),CustomProviderData("LastName;nvarchar")]
public string LastName
{
get { return base["LastName"] as string; }
set { base["LastName"] = value; }
}
public static UserProfile GetUserProfile(string username)
{
return Create(username,false) as UserProfile;
}
public static UserProfile GetUserProfile()
{
return Create(Membership.GetUser().UserName,true) as UserProfile;
}
}
}
And web.config like
<profile enabled="true" defaultProvider="TableProfileProvider" inherits="MyNamespace.Models.UserProfile">
<providers>
<clear />
<add name="TableProfileProvider" type="Microsoft.Samples.SqlTableProfileProvider" connectionStringName="ContentDB" table="aspnet_UserProfiles" applicationName="/"/>
</providers>
</profile>
Things that I think are found along the way
- Using a custom provider with MVC requires the attribute "inherits" in the element
<profile>in web.config, which eliminates the use of a construct <properties><add ....>with the same profile field name. - SQL
CustomProviderData, - web.config, .
, . , , .
MVC:
FormsService.SignIn(model.UserName, false );
UserProfile profile = UserProfile.GetUserProfile(Membership.GetUser().UserName);
profile.FirstName = "Doug";
Profile.Save();
return RedirectToAction("Index", "Home");
, Membership.GetUser() null, . .
FormsService.SignIn(model.UserName, false );
UserProfile profile = UserProfile.GetUserProfile(model.UserName);
profile.FirstName = "Doug";
profile.Save();
return RedirectToAction("Index", "Home");
, FirstName " , " (, t , ).
? , FormsServer.SignIn , , , -, , cookie .
, ( aspnet_UserProfiles....). ?
- ? ? !
, , .
, .
FormsService.SignIn(model.UserName, false );
GenericIdentity id = new GenericIdentity(model.UserName);
HttpContext.User = new GenericPrincipal(id, null);
UserProfile profile = UserProfile.GetUserProfile(Membership.GetUser().UserName) as UserProfile;
profile.FirstName = "Doug";
profile.Save();
return RedirectToAction("Index", "Home");
, , Memberhip.GetUser() MembershipUser, FirstName - This property cannot be set for anonymous users.
, , Profile .
?