Login user after signup

夙愿已清 提交于 2019-12-12 19:27:42

问题


how to auto login the user after he created an account

using asp.net 3.5, and from authentication

here is the code:

 <asp:CreateUserWizard ID="mainSignUp" runat="server" 
    CreateUserButtonText="SignUp" FinishDestinationPageUrl="copyPastPage.aspx"
     ContinueDestinationPageUrl="~/copyPastPage.aspx"
     OnCreatedUser="redirect" LoginCreatedUser="true">
    <CreateUserButtonStyle CssClass="signUpButton" />
    <TextBoxStyle BorderStyle="None" Height="35px" 
        Width="200px" />
    <WizardSteps>
        <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" >
            <ContentTemplate>
                <table>
                    <tr>
                        <td align="right">
                            <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="UserName" runat="server" BorderStyle="None" BorderWidth="1px" 
                                CssClass="signUpTextBox" Height="39px" Width="197px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                ToolTip="User Name is required." ValidationGroup="mainSignUp">*</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" BorderStyle="None" BorderWidth="1px" 
                                CssClass="signUpTextBox" Height="39px" TextMode="Password" Width="197px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                ControlToValidate="Password" ErrorMessage="Password is required." 
                                ToolTip="Password is required." ValidationGroup="mainSignUp">*</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" BorderStyle="None" 
                                BorderWidth="1px" CssClass="signUpTextBox" Height="39px" TextMode="Password" 
                                Width="197px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
                                ControlToValidate="ConfirmPassword" 
                                ErrorMessage="Confirm Password is required." 
                                ToolTip="Confirm Password is required." ValidationGroup="mainSignUp">*</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" BorderStyle="None" BorderWidth="1px" 
                                CssClass="signUpTextBox" Height="39px" Width="197px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                                ControlToValidate="Email" ErrorMessage="E-mail is required." 
                                ToolTip="E-mail is required." ValidationGroup="mainSignUp">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="Question" runat="server" BorderStyle="None" BorderWidth="1px" 
                                CssClass="signUpTextBox" Height="39px" Width="197px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" 
                                ControlToValidate="Question" ErrorMessage="Security question is required." 
                                ToolTip="Security question is required." ValidationGroup="mainSignUp">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="Answer" runat="server" BorderStyle="None" BorderWidth="1px" 
                                CssClass="signUpTextBox" Height="39px" Width="197px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" 
                                ControlToValidate="Answer" ErrorMessage="Security answer is required." 
                                ToolTip="Security answer is required." ValidationGroup="mainSignUp">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2">
                            <asp:CompareValidator ID="PasswordCompare" runat="server" 
                                ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                                Display="Dynamic" 
                                ErrorMessage="The Password and Confirmation Password must match." 
                                ValidationGroup="mainSignUp"></asp:CompareValidator>
                        </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>
        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
    </WizardSteps>
</asp:CreateUserWizard>

and the code behind :

public partial class Default3 : System.Web.UI.Page

{ protected void Page_Load(object sender, EventArgs e) { }

protected void redirect(object sender, EventArgs e) 
{

    FormsAuthentication.SetAuthCookie(USER_NAME, true);
    Response.Redirect("copyPastPage.aspx");
}

}

thanks in advance


回答1:


FormsAuthentication.RedirectFromLoginPage(mainSignUp.UserName, true);

Put the above line inside the redirect method and remove those two lines and let us know if that helps




回答2:


After a successfull Signup, just use this line:

FormsAuthentication.SetAuthCookie(USER_NAME, true);

the true at the end means (from official documentation):

true to create a persistent cookie (one that is saved across browser sessions); otherwise, false.




回答3:


If you are using CreateUserWizard control for user registration, you may set a property LoginCreatedUser to true to automatically login a user after registration is complete.



来源:https://stackoverflow.com/questions/5614343/login-user-after-signup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!