Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonas Eriksson 930 posts 1825 karma points
    Feb 15, 2011 @ 14:13
    Jonas Eriksson
    0

    Stay on protected page after login

    To make login stay on the current page after login - and not redirect to a default page, change the following part of the script:

          string username=request.Form["username"];
          string password=request.Form["password"];
          if (Membership.ValidateUser(username, password))
          {
            // FormsAuthentication.RedirectFromLoginPage(username, true);
    // Set authorization cookie persistant
            FormsAuthentication.SetAuthCookie(username, true);
            HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl);
          }
  • kows 81 posts 151 karma points c-trib
    Feb 15, 2011 @ 14:40
    kows
    0

    isn't it "safer" to use:

    HttpContext.Current.Response.Redirect(umbraco.library.NiceUrl(Node.GetCurrent().Id));

    to reset possible querystring values etc?

    I don't like using RawUrl :)

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 17, 2011 @ 12:22
    Jonas Eriksson
    0

    Hi!

    Actually I could not get that working, guess the current node id is pointing to login-page and not the requested page at the time for the macro processing(?).

    But I have now been fiddling around with the login some more, and made it a little cleaner with the new possibilities in 4.7. I would be very glad if someone could help me out with some testing (it does work in my installation):

    I'm using "Gist" for the source until it's tested enough, easier to edit it there (and easier to copy-paste from there too):

    https://gist.github.com/874194

    To use it - just paste the code into a template within <umbraco:macro runat="server" language="razor">...</umbraco:macro>,

    or create a macro.

  • Amir Khan 1284 posts 2741 karma points
    Apr 21, 2011 @ 00:14
    Amir Khan
    0

    Awesome package, this makes working with membership so much easier! How would I configure it to redirect to the protected page instead of staying on the current one? I have the login form on every page of the site in the header...

    Thanks for any help!

    Amir

  • Jonas Eriksson 930 posts 1825 karma points
    Apr 27, 2011 @ 19:36
    Jonas Eriksson
    0

    Glad you like it :)

    Hm I will have to try it with your configuration. Did you use the code from the gist-link?

     

  • Amir Khan 1284 posts 2741 karma points
    Apr 27, 2011 @ 21:19
    Amir Khan
    0

    Yep! Heres my code if it helps.

    @using System.Web  
    @using System.Web.Security
    @helper LoginForm()
    {
     <form id="loginForm" method="post">
                          <div id="loginBody">
                          <ul id="loginList">
                            
                                    <li><label for="email">Email Address</label>
                                      <input type="text" name="username" id="username" /></li>
                                    <li><label for="password">Password</label>
                                      <input type="password" name="password" id="password" /></li>
                              <li><input type="submit" id="submit" value="Sign in" /></li>
                            
                          </ul>
                         </div>
                        </form>
    }

    @helper LogoutForm()
    {
      <form method="post">
        <input type="submit" id="submit" name="submit" value="logout"/>
      </form>
    }

    @helper Message(string message)
    {
      <p>@message</p>
    }



    @{
      var isSubmitLogin = (IsPost && Request["submit"]=="login");
      var isSubmitLogout = (IsPost && Request["submit"]=="logout");
      var currentUser = Membership.GetUser();
      var requestedUrl = Request.Url.PathAndQuery.ToString(); // Model.Url;
      if (Request["ReturnUrl"]!=null)
      {
        requestedUrl = Request["ReturnUrl"];
      }

      if (currentUser!=null)
       {    
        if (!isSubmitLogout)
         {
           @Message("Logged in : " + currentUser.UserName)
           @LogoutForm()
         }
        else
         {
           FormsAuthentication.SignOut();
           FormsAuthentication.RedirectToLoginPage();
         }
       }

      if (currentUser==null)
       {
        if (!isSubmitLogin)  
         {
           @LoginForm()
          }
        else
         {
          string username=Request["username"];
          string password=Request["password"];
          if (Membership.ValidateUser(username, password))
          {
            // RedirectFromLoginPage does not work that good within the Umbraco context
            // FormsAuthentication.RedirectFromLoginPage(username, true);

            FormsAuthentication.SetAuthCookie(username, true);

            // Redirect to / refresh the requested page
            Response.Redirect(requestedUrl);
          }
          else
          {
            @Message("Login failed for " + username)
            @LoginForm()
          }
         }       
        }
      }

     

Please Sign in or register to post replies

Write your reply to:

Draft