Search code examples
asp.net-mvcentity-frameworksimplemembership

Redirect after Register Using UserID


using VS'12 asp.net MVC4 C# InternetApplication with KendoUI and Simplemembership EF Code first.

this is what VS gives you to start out with.

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
. 
.
.

Here is where i would like it to RedirectToAction to my Controller ChangeUsersInfoController, and send it the id of the newly created user.

To get the ID i have tried

1.

               var UserID = (from u in db.UserProfiles
                            where u.UserName == User.Identity.Name
                            select new
                            {
                               UserID = u.UserID
                            }).Single();

2.

  var userID = System.Web.Security.Membership.GetUser().ProviderUserKey;
  1. return RedirectToAction("Edit","ChangeUsersInfo", userID);

  2. I have also Tried return RedirectToAction("Edit","ChangeUsersInfo"); to see if it would let me jsut send it there without a variable.

For some reason

  1. variable returned = Null
  2. variable returned = Null
  3. doesnt work because 1 and 2 didnt
  4. seems to not redirect to the new EDIT but to the LOG IN Screen

What is the reason behind my nulls and the fail Redirect?

Yes the database has users


Solution

  • var userID = System.Web.Security.Membership.GetUser().ProviderUserKey;
    

    Using the WebSecurity and System.Web.Security items will only work on subsequent request because you have not sent the auth cookie back to the client yet (this is where it pulls its data from). Given that, you should not even have to send the user Id of the user in the redirect URL...they are logged in and you can just get it at the controller/action that you are going to. By grabbing the userId from the auth ticket, you now do not have to check if the user Id passed in the URL is actually the user that is currently logged in.

    Example:

    Login Method

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                 //send em back with the errors
                 Return View();
            }
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "ChangeUsersInfo");
            }...
        }
    

    Change User Info Controller

    [Authorize]
    public ActionResult Index()
    {
        var userId = WebSecurity.CurrentUserId;
        var model = GetUserInfoService.GetUserInfo(userId);
        Return View(model);
    }