Custom Membership Provider中自定义字段的值

时间:2012-02-07 10:18:01

标签: c# .net asp.net-membership membership-provider profile-provider

我通过继承.NETs Membership Provider实现了自定义成员资格提供程序。对于我的前端使用CreateUserWizard Control,它与我的会员提供商连接。我编辑了CreateUserWizard控件的模板,现在它看起来像这样:

 <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
                    MembershipProvider="CmsMembershipProvider" BackColor="#E3EAEB" 
                    BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px" 
                    Font-Names="Verdana" Font-Size="0.8em" 
                    ContinueDestinationPageUrl="~/Pages/FirstPage.aspx">
                    <ContinueButtonStyle BackColor="White" BorderColor="#C5BBAF" 
                        BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" 
                        ForeColor="#1C5E55" />
                    <CreateUserButtonStyle BackColor="White" BorderColor="#C5BBAF" 
                        BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" 
                        ForeColor="#1C5E55" />
                    <TitleTextStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <WizardSteps>
                        <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                            <ContentTemplate>
                                <table>
                                    <tr>
                                        <td align="center" colspan="2" class="style3">
                                            <strong>Sign Up for Your New Account</strong></td>
                                    </tr>
                                    <tr>
                                        <td align="right">
                                            <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="UserName" name="usna" runat="server"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                                ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                                ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right">
                                            <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                                ControlToValidate="Password" ErrorMessage="Password is required." 
                                                ToolTip="Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right">
                                            <asp:Label ID="ConfirmPasswordLabel" runat="server" 
                                                AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
                                                ControlToValidate="ConfirmPassword" 
                                                ErrorMessage="Confirm Password is required." 
                                                ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right">
                                            <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                                                ControlToValidate="Email" ErrorMessage="E-mail is required." 
                                                ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                        </td>
                                    </tr>
                                     <tr>
                                        <td align="right">
                                            <asp:Label ID="DomainLabel" runat="server" AssociatedControlID="Domain">Domain:</asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="Domain" runat="server"></asp:TextBox>
                                            <asp:RequiredFieldValidator ID="DomainRequired" runat="server" 
                                                ControlToValidate="Domain" ErrorMessage="Domain is required." 
                                                ToolTip="Domain is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="center" colspan="2" style="color:Red;">
                                            <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                                        </td>
                                    </tr>
                                </table>
                            </ContentTemplate>
                            <CustomNavigationTemplate>
                                <table border="0" cellspacing="5" style="width:100%;height:100%;">
                                    <tr align="right">
                                        <td align="right" colspan="0">
                                            <asp:Button ID="StepNextButton" runat="server" BackColor="White" 
                                                BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" 
                                                CommandName="MoveNext" Font-Names="Verdana" ForeColor="#1C5E55" 
                                                Text="Create User" ValidationGroup="CreateUserWizard1" />
                                        </td>
                                    </tr>
                                </table>
                            </CustomNavigationTemplate>
                        </asp:CreateUserWizardStep>
                        <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                        </asp:CompleteWizardStep>
                    </WizardSteps>
                    <FinishNavigationTemplate>
                        <asp:Button ID="FinishPreviousButton" runat="server" BackColor="White" 
                            BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" 
                            CausesValidation="False" CommandName="MovePrevious" Font-Names="Verdana" 
                            ForeColor="#1C5E55" Text="Previous" />
                        <asp:Button ID="FinishButton" runat="server" BackColor="White" 
                            BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" 
                            CommandName="MoveComplete" Font-Names="Verdana" ForeColor="#1C5E55" 
                            Text="Finish" />
                    </FinishNavigationTemplate>
                    <HeaderStyle BackColor="#666666" BorderColor="#E6E2D8" BorderStyle="Solid" 
                        BorderWidth="2px" Font-Bold="True" Font-Size="0.9em" ForeColor="White" 
                        HorizontalAlign="Center" />
                    <NavigationButtonStyle BackColor="White" BorderColor="#C5BBAF" 
                        BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" 
                        ForeColor="#1C5E55" />
                    <SideBarButtonStyle ForeColor="White" />
                    <SideBarStyle BackColor="#1C5E55" Font-Size="0.9em" VerticalAlign="Top" />
                    <StartNavigationTemplate>
                        <asp:Button ID="StartNextButton" runat="server" BackColor="White" 
                            BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" 
                            CommandName="MoveNext" Font-Names="Verdana" ForeColor="#1C5E55" Text="Next" />
                    </StartNavigationTemplate>
                    <StepNavigationTemplate>
                        <asp:Button ID="StepPreviousButton" runat="server" BackColor="White" 
                            BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" 
                            CausesValidation="False" CommandName="MovePrevious" Font-Names="Verdana" 
                            ForeColor="#1C5E55" Text="Previous" />
                        <asp:Button ID="StepNextButton" runat="server" BackColor="White" 
                            BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" 
                            CommandName="MoveNext" Font-Names="Verdana" ForeColor="#1C5E55" Text="Next" />
                    </StepNavigationTemplate>
                    <StepStyle BorderWidth="0px" />
                </asp:CreateUserWizard>

它有原始的fieds减去SecurityQuestion和Answer,但另外我有字段域是文本框。

我有覆盖方法CreateUser,所以看起来像这样:

public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {

        ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);
        OnValidatingPassword(args);

        AccountDB accountDB = new AccountDB();

        string usn = username;
        string eml = email;
        string passSalt = AccountDB.CreateSalt();
        string pass = AccountDB.CreatePasswordHash(password, passSalt);  
        DateTime crtDt = DateTime.Now;
        DateTime lstMdfDt = DateTime.Now;
        DateTime lstLgnDt = DateTime.Now;
        string lstLgnIp = (string)HttpContext.Current.Request.UserHostAddress;
        byte isAct = 0;
        string cmnt = "komentar";

        AccountDetails accountDetails = new AccountDetails(usn, eml, pass, passSalt, crtDt, lstMdfDt, lstLgnDt, lstLgnIp, isAct, cmnt);

        if (args.Cancel)
        {
            status = MembershipCreateStatus.InvalidPassword;
            return null;
        }
        if(RequiresUniqueEmail && !accountDB.IsEmailUnique(email))
        {
            status = MembershipCreateStatus.DuplicateEmail;
            return null;
        }
        if (!accountDB.IsUsernameUnique(username))
        {
            status = MembershipCreateStatus.DuplicateUserName;
            return null;
        }
        else 
        {
            status = MembershipCreateStatus.Success;
            int idOfInsertedUser = accountDB.InsertAccount(accountDetails);

            int lanId = 1;
            int pySclId = 1;
            int appTypId = 1;
            int indSecId = 1;
            int thmId = 1;
            string appName = "ApplicationName";

           // ApplicationDetails applicationDetails = new ApplicationDetails(0,lanId, pySclId, idOfInsertedUser, appTypId, indSecId, thmId, appName);
         //   ApplicationDB applicationDB = new ApplicationDB();
           // int idOfApplication = applicationDB.InsertApplication(applicationDetails);


            //DomainDB domainDB = new DomainDB();
          // This is where i want to have value of textBox Domain in variable domainName
            //DomainDetails domainDetails = new DomainDetails(0, idOfApplication, domainName);
            //domainDB.InsertDomain(domainDetails); 
            return accountDB.GetAccount(idOfInsertedUser);
        }

    }

它完美地插入数据库,但我无法从表单中获取TextBox Domain的值,因为Create User不会发送该变量。 我发现一些例子说,我可以在CreateUserWizard中创建另一个步骤,然后使用新的值集更新数据库,但我不想在不同的步骤中执行此操作。可以一步完成吗?

1 个答案:

答案 0 :(得分:1)

由于您有其他用户的额外信息,因此您可能需要考虑创建Custom Membership User。然后,您可以向CreateUser方法提供另一个覆盖以插入该信息。