一旦用户按会员资格创建新帐户,我该如何进行身份验证? Asp.Net中的角色

时间:2012-05-10 06:23:21

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

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

这是我的代码。

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;

    }

3 个答案:

答案 0 :(得分: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?
            }

        }

答案 1 :(得分:1)

如果您使用FormsAuthentication:

FormsAuthentication.SetAuthCookie(userName, isPrsistent);

如果您使用Asp.Net MVC,那么:

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

对于WebForms,请参阅user1237131 answer

答案 2 :(得分: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);
          }
 }

希望它可以帮助你:)