1

I'm modifying an asp.net MVC application where Razor is used in the .cshtml. In the header is a call to the function "RendoWelcomeMessage()". This calls a function in a script on the same page. Normally, it displays the user's name. If the session has timed out, it displays a message saying to login. I need to change it so that if the session times out it redirects to the login page. The code below doesn't work. It seems to skip or ignore the redirect and just returns the message. How do I get the redirect to work?

Here's the call to the function in the HTML header:

<header>
    <div class="content-wrapper">
        <div class="float-right" style="font-size: 10pt;">
            <b>@RenderWelcomeMessage()</b>@Html.Raw(@RenderUser())
        </div>
    </div>
</header>

Here's the function:

@functions
{
public string RenderWelcomeMessage()
{
    string welcomeMessage = "Please Log In";
    try
    {
        welcomeMessage = Session["CurrLoggedUser"] == null ? "Please Log In" : "Welcome: ";
    }
    catch (Exception ex)
    {
        Response.Redirect("~/Account/Login");
        //return "Please Log In";
    }
    return welcomeMessage;
}
}

It's definitely hitting the part in the catch, but it's behaving as if I'd never added the response.redirect.

boilers222
  • 1,901
  • 7
  • 33
  • 71
  • 4
    Your code to redirect belongs in the controller GET method, not the view –  Jan 24 '17 at 20:51
  • I know, but a coworker get this up inside the view. It breaks too many things if I start changing it. I'm just working with what I have. Any suggestions? – boilers222 Jan 24 '17 at 22:11
  • How could it possible 'break' anything. In the controller GET method - `if (Session["CurrLoggedUser"] == null) { return RedirectToAction("Login", "Account"); }` But its fairly clear that you authorization is not set up correctly because if it was, this would all be handled out of the box by adding the `[Authorize]` attribute to that method. –  Jan 24 '17 at 22:17
  • Yes, authorization is not used at all. My coworker didn't know how so he worked around it. I'm trying to fix this with no time budgeted for changes. There's got to be someone to redirect from the view inside the @functions. If you know of a way, please let me know. Thanks! – boilers222 Jan 25 '17 at 13:13

1 Answers1

1

The solution can be done by moving rendering to action with Authorize attribute.

Move RenderWelcomeMessage to action

<header>
    <div class="content-wrapper">
        <div class="float-right" style="font-size: 10pt;">
            <b>@Html.Action("WelcomeMessage")</b>
        </div>
    </div>
</header>

Create an action

 [Authorize]
 public ActionResult WelcomeMessage()
 {
     return Content("Welcome: " + Session["CurrLoggedUser"]);
 }

When session will be expired user will be redirected to login page automatically (of course if it is configured in application).

Community
  • 1
  • 1
Vasyl Zvarydchuk
  • 3,789
  • 26
  • 37
  • Where is that action? In the controller? The RenderWelcomeMessage is in a layout view (Views/Shared/_Layout.cshtml). There's no SharedController, so where would the action be? – boilers222 Jan 24 '17 at 22:13
  • You can use existed controller or create a new one. Using Authorize attribute is more reliable way to check if user is logged-in or not. – Vasyl Zvarydchuk Jan 25 '17 at 04:09
  • Thanks. Yes using the authorize syntax would be better, but my coworker didn't set it up that way. We're supposed to be going live and I'm trying to fix little issues like this while spending as little time as possible on this. So if I have a view, say Home/Main.cshtml which uses the controller HomeController.cs and uses _Layout.cshtml as it's layout, can I put an action in HomeController.cs and have _Layout.cshtml reference it? – boilers222 Jan 25 '17 at 13:10
  • Yes, you can put any action from any controller to _Layout.cshtml but you need to pass controller name too. Like in this way `@Html.Action("WelcomeMessage", "Home")` where `WelcomeMessage` - action name and `Home` - controller name (`HomeController` class). – Vasyl Zvarydchuk Jan 25 '17 at 13:24