在 CreatedUser 事件上填写个人资料会得到:无法为匿名用户设置此 属性。?

Filling the Profile on CreatedUser event gets: This property cannot be set for anonymous users.?

我正在尝试用来自用户的数据填充 Profile 属性,就在访问该网站之后。

ASPX:

<form id="form1" runat="server">
<div class="center SignUp">
    <asp:CreateUserWizard ID="CreateUserWizard" runat="server" OnCreatedUser="CreateUserWizard_CreatedUser">
        <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
            </asp:CreateUserWizardStep>
            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
            </asp:CompleteWizardStep>
        </WizardSteps>
    </asp:CreateUserWizard>
</div>
</form>

代码隐藏:OnCreatedUser 事件

protected void CreateUserWizard_CreatedUser(object sender, EventArgs e)
{
    MembershipUser newUser = System.Web.Security.Membership.GetUser(CreateUserWizard.UserName);
    Profile.UserName = newUser.UserName;     // Get and Set the user name after sign up.
    Guid Id = (Guid)newUser.ProviderUserKey; // Get the UserId after sign up.
    Profile.UserId = Id;                     // Set the UserId after sign up.
}

填写数据后点击"Create User"。我得到:

Server Error in '/' Application.

This property cannot be set for anonymous users.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Configuration.Provider.ProviderException: This property cannot be set for anonymous users.

Source Error:

Line 40: } Line 41: set { Line 42:
this.SetPropertyValue("UserName", value); Line 43: } Line 44: }

Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\b2622a96\eb730107\App_Code.ef53d8ra.4.cs Line: 42

Stack Trace:

[ProviderException: This property cannot be set for anonymous users.] System.Web.Profile.ProfileBase.SetInternal(String propertyName, Object value) +2383575 System.Web.Profile.ProfileBase.set_Item(String propertyName, Object value) +80
System.Web.Profile.ProfileBase.SetPropertyValue(String propertyName, Object propertyValue) +13 ProfileCommon.set_UserName(String value) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\b2622a96\eb730107\App_Code.ef53d8ra.4.cs:42
SignUp.CreateUserWizard_CreatedUser(Object sender, EventArgs e) in c:\BegASPNET\FacebookCorrectReation\SignUp.aspx.cs:18
System.Web.UI.WebControls.CreateUserWizard.OnCreatedUser(EventArgs e) +116 System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() +342 System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) +110 System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) +401
System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) +119
System.Web.UI.WebControls.WizardChildTable.OnBubbleEvent(Object source, EventArgs args) +16
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +252
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408

CreateUserWizard_CreatedUser 事件处理程序的开头添加此行:

Profile.Initialize(CreateUserWizard.UserName,true); 这样就变成了:

protected void CreateUserWizard_CreatedUser(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        Profile.Initialize(CreateUserWizard.UserName,true);
        MembershipUser newUser = System.Web.Security.Membership.GetUser(CreateUserWizard.UserName);
        Profile.UserName = newUser.UserName;     // Get and Set the user name after sign up.
        Guid Id = (Guid)newUser.ProviderUserKey; // Get the UserId after sign up.
        Profile.UserId = Id;                     // Set the UserId after sign up.
    }
}