如何为新的用户配置文件数据 [英] How to set profile data for new user
问题描述
我有一个MVC3的网站,我写code注册一个用户。在code做到这一点:
I have an MVC3 site and I am writing code to register a user. The code does this:
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
它的下一件事情是这样的:
The next thing it does is this:
HttpContext.Profile["FirstNAme"] = model.FirstName;
HttpContext.Profile["LastName"] = model.LastName;
这是它失败。我得到的错误是:
This is where it fails. The error I get is:
此属性不能为匿名用户进行设置。
This property cannot be set for anonymous users.
我明白为什么;这是因为不存在用户登录或没有用户指定的,所以当然我无法设置为用户的配置文件。我从开始临ASP.NET MVC 3框架754页的工作,这就是我从这个code。我在web.config文件中设置起来。
I understand why; it is because there is no user logged in or no user specified, so of course I can't set the profile for the user. I am working from page 754 onwards of Pro ASP.NET MVC 3 Framework and this is where I got this code from. I have set this up in the web.config file.
<profile enabled="true" defaultProvider="AspNetSqlProfileProvider">
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="FirstName" type="String"/>
<add name="LastName" type="String"/>
</properties>
</profile>
我的问题是我怎么设置我尝试注册用户的姓氏和名字?
My question is how do I set the first name and last name of the user I am trying to register?
推荐答案
尝试通过获取配置文件的用户的用户名:
Try getting the profile for that user's username by:
ProfileBase profile = ProfileBase.Create(model.UserName);
是,打造'得到'你刚才创建的用户的配置文件,而不仅仅是创建它(误导!)。那么你可以使用:
Yes, create 'gets' the profile for the user you just created, not just creates it (misleading!). Then you can use:
profile["FirstName"] = model.FirstName;
profile["LastName"] = model.LastName;
不要忘了叫 profile.Save();
设定值后,
这篇关于如何为新的用户配置文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!