0

我在 Asp.Net Membership 中创建了一个自定义 CreateUser。现在我想要的是在创建用户时它应该被重定向为 default.aspx 页面上的登录用户。我如何验证并重定向到默认页面。我不希望他再次登录,但是当用户成功创建时,它应该自动登录并转到默认页面。

这是我的代码。

public string AddUser(string UserName, string FirstName, string LastName, string Gender, string EmailID, string Password, string Mobile, string SecurityQuestion, string SecurityAnswer)
    {
        MembershipCreateStatus CreateStatus;

        MembershipUser newUser = Membership.CreateUser(UserName, Password, EmailID, SecurityQuestion, SecurityAnswer, true, out CreateStatus);

        if (CreateStatus == MembershipCreateStatus.Success)
        {

            Guid newUserId = (Guid)newUser.ProviderUserKey;

            if (RegisterCustomFields(newUserId, FirstName, LastName, Gender, Mobile) != 0)
            {
                Roles.AddUserToRole(newUser.UserName, "User");
                ReturnMsg = "Successfully Created ";
            }
            else
            {
                Membership.DeleteUser(newUser.UserName);
                ReturnMsg = "Error Creating User";
            }

        }
        else
        {

            switch (CreateStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    ReturnMsg = "There already exists a user with this username.";
                    break;

                case MembershipCreateStatus.DuplicateEmail:
                    ReturnMsg = "There already exists a user with this email address.";
                    break;
                case MembershipCreateStatus.InvalidEmail:
                    ReturnMsg = "There email address you provided in invalid.";
                    break;
                case MembershipCreateStatus.InvalidAnswer:
                    ReturnMsg = "There security answer was invalid.";
                    break;
                case MembershipCreateStatus.InvalidPassword:
                    ReturnMsg = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
                    break;
                default:
                    ReturnMsg = "There was an unknown error; the user account was NOT created.";
                    break;
            }


        }

        return ReturnMsg;

    }
4

3 回答 3

1

注册成功后,编写以下代码。server.Transfer("~/Default.aspx");

对于身份验证:不确定您是否想知道他们是“注册”用户还是已登录(如登录状态所示)

这是要知道他们是否已登录(登录状态使用什么):

System.Web.HttpContext.Current.User.Identity.IsAuthenticated;

试试这个:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if(Membership.ValidateUser(Login1.UserName, Login1.Password))
            {
                Response.Redirect("default.aspx");
                // Set the user as logged in?
            }

        }
于 2012-05-10T06:28:20.597 回答
1

如果您使用 FormsAuthentication :

FormsAuthentication.SetAuthCookie(userName, isPrsistent);

如果您使用 Asp.Net MVC,则:

return RedirectToAction("Index", "Home");

对于 WebForms,请参阅 user1237131 答案

于 2012-05-10T06:29:44.100 回答
0

由于您已注册用户但未验证登录成功后用户不会自动登录

因此,在您发现用户已成功注册到应用程序并在注册后将他添加到特定角色后。然后您可以使用以下代码验证用户。

 if (Membership.ValidateUser(UserName, password.Text))
      {
      string[] role = Roles.GetRolesForUser(username.Text);
      string userrole = role[0].ToString();

      FormsAuthentication.SetAuthCookie(UserName, true);
      // Above line will create a Cookie on Browser and you can use this to check the authentication of the user. 
      if (userrole == "User")
          {
              Response.Redirect("~/Default.aspx", true);
          }
      else
          {
              Response.Redirect("~/UnAuthorizedPage.aspx", true);
          }
 }

希望它可以帮助你:)

于 2012-05-10T06:39:21.353 回答