Reputation: 77
I have created a custom CreateUser in Asp.Net Membership. Now what i want is when User is created it should be redirected as a loggedIn user on default.aspx page. how can i Authenticate and Redirect to Default page. I dont want him to Login Again but when user is created successfully it should automatically logged in and go to Default page.
Here is my code.
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;
}
Upvotes: 0
Views: 761
Reputation: 1863
After Registration was success write the below code. server.Transfer("~/Default.aspx");
For Authentication: Not sure if you want to know if they are a "registered" user or have logged in (as the Login status would indicate)
Here's to know if they are logged in (what the Login status uses):
System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
Try This:
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?
}
}
Upvotes: 1
Reputation: 3065
Since you have registered the user but not Validated it for login after successfully registration user is not logged in automatically
Therefore after you find that the user has register to the Application successfully and after registration he is added to a specific role. Then you can validate the User with use of the below code.
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);
}
}
Hope it helps you :)
Upvotes: 0
Reputation: 3236
If you use FormsAuthentication :
FormsAuthentication.SetAuthCookie(userName, isPrsistent);
If you use Asp.Net MVC then:
return RedirectToAction("Index", "Home");
For WebForms see user1237131 answer
Upvotes: 1