0

If the user login is valid, i will redirect the view to another view as shown below. The following method is in a Controller class.

 if (loginSuccess(email,pwd))
 {                   
    FormsAuthentication.SetAuthCookie(email, false);
    return RedirectToAction("Index", "SuccessPage");   

}

Now i want to display the username on the redirected view (which is /SuccessPage/Index). How can i do this ?

Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140

4 Answers4

3

You can pass parameters in the redirect.

if (loginSuccess(email,pwd))
 {                        
    string name = User.Identity.Name;
    return RedirectToAction("Index", "SuccessPage"), new {username = name };   
}

(Obviously, the controller action needs to have access to the username attribute before it can pass the value)

Note: The example above is useful for understanding how to pass additional parameters in a redirect, but in this case it's overkill since User.Identity.Name is available in the session once a user has authenticated and you can access it from any controller or view, so no need to pass (see Josh's response).

Community
  • 1
  • 1
Jack
  • 1,319
  • 8
  • 16
  • No, the successpage is in another controller. How can i access username from the other controller ? – Sharon Watinsan Feb 22 '13 at 19:33
  • You can pass the username in the redirect as above. I've edited it to show one way of getting the logged in user's name – Jack Feb 22 '13 at 19:34
  • Why pass the data around, when it is freely available already? – Josh Mein Feb 22 '13 at 19:43
  • 1
    It's useful for the OP to know how to do it in case there's a need to pass other data that isn't available in the session. – Jack Feb 22 '13 at 19:48
  • I agree that your answer would be helpful in other cicumstances, but I was trying to encourage you to add that specification in your answer. If an inexperienced user looks at your answer to solve the same scenario, they could be mislead into doing something unnecessary. – Josh Mein Feb 22 '13 at 20:14
2

When a user is authenticated, you can use the HttpContext to get basic user information. For example, you can use the following to get the username:

string userName = HttpContext.User.Identity.Name;

You don't need to worry about passing the information between controllers. The information will be available as long as the user is autheticated.

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
1

In the SuccessPage.Index action, get the user's name and pass it into the view via the model.

To get the user's name, you either get it from a saved Session variable, or you can use controller.HttpContext.User.Identity.Name.

gabnaim
  • 1,103
  • 9
  • 13
1

You have 4 options if we're just discussing passing data. (If we're talking about just the username then you want Josh's response)

Passing parameters in the redirect (as Jack suggested)

return RedirectToAction("Index", "SuccessPage"), new {username = name };

Storing it in temp data (only works for 1 request)

TempData[Key] = name;
return RedirectToAction("Index", "SuccessPage");

Storing it in session (lasts as long as the session lasts)

Session[Key] = name;
return RedirectToAction("Index", "SuccessPage");   

Storing it in the database and linking that data via their session id.

/* databasey code here */ 
return RedirectToAction("Index", "SuccessPage");   

That's your full set of options from simplest to most complex. I'd suggest in your case you just pass the values in the URL (first one) as your system expands and grows you may want to consider trying out the other options.

It's worth noting that TempData doesn't last across a page refresh.

Community
  • 1
  • 1
Quibblesome
  • 25,225
  • 10
  • 61
  • 100
  • 1
    Honestly, you might as well not count the first option. Why pass data that is freely available otherwise? – Josh Mein Feb 22 '13 at 19:42
  • Well it depends on if we're answering the title of the question or the contents. Your answer is the best one for the contents so I linked it. – Quibblesome Feb 22 '13 at 19:45
  • 1
    @Quibblesome TempData now lasts until the value is read from the tempdatadictionary (since MVC2). more info in my answer here: http://stackoverflow.com/a/14967113/2040555 – Jack Feb 22 '13 at 19:46
  • 1
    @Jack that's useful to know but it still means that a redirect that reads it that then refreshes will find no tempdata and potentially fall on its ass. – Quibblesome Feb 22 '13 at 19:50
  • @Quibblesome Absolutely agree. – Jack Feb 22 '13 at 19:54