【发布时间】:2011-03-06 12:33:45
【问题描述】:
我正在尝试将来自 http://www.asp.net/downloads/sandbox/table-profile-provider-samples 的示例表配置文件提供程序添加到新的 MVC 2 站点。
经过一番研究和摆弄,我得到了一个看起来像这样的配置文件类。
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;
}
}
}
还有一个类似的 web.config
<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>
我认为我在此过程中发现的事情是
- 在 MVC 中使用自定义提供程序需要 web.config 中
<profile>元素的“inherits”属性,这会阻止使用具有相同配置文件字段名称的<properties><add ....>构造。 - 示例 SQL 表配置文件提供程序需要
CustomProviderData属性,由于上述原因,它不能出现在 web.config 文件中,因此需要作为属性添加到配置文件类的属性中。李>
似乎一切正常一旦用户登录。但是,我想在新用户注册过程中捕获一些配置文件数据,并且在用户登录之前我似乎无法访问配置文件对象。
我已尝试在 MVC 模板代码的新用户注册部分添加一个调用以保存配置文件数据:
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
UserProfile profile = UserProfile.GetUserProfile(Membership.GetUser().UserName);
profile.FirstName = "Doug";
Profile.Save();
return RedirectToAction("Index", "Home");
但是,在用户实际登录之前,Membership.GetUser() 似乎为空。我也尝试使用模型中的用户名。
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
UserProfile profile = UserProfile.GetUserProfile(model.UserName);
profile.FirstName = "Doug";
profile.Save();
return RedirectToAction("Index", "Home");
这更进一步,但在尝试设置 FirstName 配置文件字段时失败,并显示错误消息“尝试将属性设置为匿名用户,但这是不允许的”(对不起,不要'在我输入此内容时无法访问确切的消息)。
有没有办法解决这个问题?就表单身份验证而言,看起来FormsServer.SignIn 方法实际上并没有让用户登录,它需要往返才能完全登录,大概需要将 cookie 提交回服务器。
如果没有简单的方法,我可以使用数据访问方法(插入 aspnet_UserProfiles ....)直接填充配置文件表。这是一座太远的桥梁,还是一个可行的解决方案?
没有人遇到过这个问题吗?不?那就只有我了!
为了更新,我已经尝试了 Franci Penov 在他对 posting 的回答中提出的建议。
所以,现在我的代码如下所示。
FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
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");
现在,至少对 Membership.GetUser() 的调用会返回一个有效的 MembershipUser 对象,但尝试设置 FirstName 配置文件属性仍会导致消息 This property cannot be set for anonymous users.
因此,就会员资格而言,用户已登录,但配置文件系统仍然认为没有。
有什么想法吗?
【问题讨论】:
-
好东西,很有用,谢谢。只想补充一点,您可能希望在自定义配置文件类的属性设置器中包含 this.Save() 方法。当我试图弄清楚为什么我的更改没有持续存在时,我发现这很有帮助。
-
嗨@Doug,很棒的帖子!我想知道您是否使用 tableprofileprovider 创建了匿名识别解决方案。当用户通过身份验证时,它似乎工作得很好,但我不知道如何与匿名用户合作,以便在他们注册之前登录他们的个人资料,比如语言等......
标签: asp.net-mvc-2 profile provider