Copied to clipboard

Flag this post as spam?

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


  • Rehan Zahid 31 posts 52 karma points
    Nov 05, 2012 @ 23:20
    Rehan Zahid
    0

    Using razor for membership login

    Hi There,

    is there anyone who knows how to make membership login by using Razor...?

  • Rune Grønkjær 1371 posts 3102 karma points
    Nov 06, 2012 @ 08:05
    Rune Grønkjær
    0

    Hi Rehan,

    We use the below couple of methods for login/logout. As you can see they rely on a form post to work, but you can probably use part of it for your project:

        [RestExtensionMethod( returnXml = false )]
        public static void Login() {
    
          HttpContext hc = HttpContext.Current;
          string loginCprno1 = hc.Request.Form[ "loginCprno1" ];
          string loginCprno2 = hc.Request.Form[ "loginCprno2" ];
          string loginPassword = hc.Request.Form[ "loginPassword" ];
          string username = loginCprno1 + loginCprno2;
          if ( ValidateUser( username, loginPassword ) ) {
            FormsAuthentication.SetAuthCookie( username, false );
            //Login success
            HttpContext.Current.Response.Redirect( HttpContext.Current.Request.UrlReferrer.ToString() );
          } else {
            //Login failed
            //TODO: skal sikkert sende noget med tilbage om login failed
            HttpContext.Current.Response.Redirect( umbraco.library.NiceUrl( 1326 ) );
          }
        }
    
        [RestExtensionMethod( returnXml = false )]
        public static void Logout() {
    
          try {
            int memberID = Member.CurrentMemberId();
            Member.ClearMemberFromClient( memberID );
          } catch {
    
          }
          FormsAuthentication.SignOut();
          HttpContext.Current.Session.Abandon();
    
          // clear authentication cookie
          HttpCookie cookie1 = new HttpCookieFormsAuthentication.FormsCookieName, string.Empty );
          cookie1.Expires = DateTime.Now.AddYears( -1 );
          HttpContext.Current.Response.Cookies.Add( cookie1 );
    
          FormsAuthentication.RedirectToLoginPage();
    
          HttpContext.Current.Response.Redirect( HttpContext.Current.Request.UrlReferrer.ToString() );
        }
    

    /Rune

  • Rehan Zahid 31 posts 52 karma points
    Nov 06, 2012 @ 09:08
    Rehan Zahid
    0

    Thx Rune...

    But this method ValidateUser is it Teacommerce method or custom method...? 

    And this code will be triggered in codebehind right...? 

     

     

     

     

     

     

  • Rune Grønkjær 1371 posts 3102 karma points
    Nov 06, 2012 @ 09:09
    Rune Grønkjær
    0

    Aah yes.

    private static bool ValidateUser( string username, string password ) {
          bool isValid = false;
          MembershipUser msu = Membership.GetUser( username );
          if ( msu != null ) {
            Member member = new Member( (int)msu.ProviderUserKey );
            isValid = Membership.ValidateUser( username, password );
    
            //Check if the member have been deactivated
            if ( isValid && member.getProperty( "memberDeactivatedDate" ).Value.ToString() != "" ) {
              isValid = false;
            }
          }
          return isValid;
        }

    /Rune

  • Rehan Zahid 31 posts 52 karma points
    Nov 06, 2012 @ 09:48
    Rehan Zahid
    0

    perfect thx...

  • merves 23 posts 43 karma points
    Dec 01, 2012 @ 20:10
    merves
    0

    Hi Rehan,

    Where should be those codes, in which file?

    Thanks.

  • Rune Grønkjær 1371 posts 3102 karma points
    Dec 03, 2012 @ 08:15
    Rune Grønkjær
    0

    Hi Merves,

    I put it in a class that I call Base as I use it as an Umbraco rest extension. You can read about it here: http://our.umbraco.org/wiki/reference/umbraco-base/simple-base-samples

    Btw. if you add the [RestExtension( "teacommerce" )] and [RestExtensionMethod( returnXml = false )] attributes you do not have to add anything to your restextensions.config

    /Rune

  • Fuji Kusaka 2203 posts 4220 karma points
    Dec 03, 2012 @ 10:28
    Fuji Kusaka
    0

    Hi Rehan, 

    You could  also make use of this package Razor Login or may be make use of this code.

     

    //fuji

  • Jebastin 42 posts 91 karma points
    Nov 18, 2014 @ 10:26
    Jebastin
    0
    @using System.Web 
    @using System.Web.Security
    @if (showLoginForm) {
      <form method="post">
        <div><label for="name">Username:</label>
        <input type="text" id="username" name="username"/></div>
        <div><label for="password">Password:</label>
        <input type="password" id="password" name="password"/></div>
        <div><input type="submit" id="submit" name="submit" value="Login"/></div>
      </form>
    }
    
    @if (showLogoutForm) {
      <form method="post">
        <input type="submit" id="submit" name="submit" value="Logout"/>
      </form>
    }
    
    @if (showMessage) {
      <p>@message</p>
    }
    
    <style type="text/css">
     p,label {color:black;}
    </style>
    
    @functions{
    
      private bool showLoginForm;
      private bool showLogoutForm;
      private bool showMessage;
      private string message;
    
      protected override void InitializePage()
      {
    
      var isSubmitLogin = (IsPost && Request["submit"]=="Login");
      var isSubmitLogout = (IsPost && Request["submit"]=="Logout");
      var isLoggedIn = HttpContext.Current.User.Identity.IsAuthenticated;
      var currentUser = Membership.GetUser();
      var requestedUrl = Request.Url.PathAndQuery.ToString(); // default get back to same page after valid login;
      if (Request["ReturnUrl"]!=null)
      {
        requestedUrl = Request["ReturnUrl"];
      }
    
      if (isLoggedIn)
       {   
        if (!isSubmitLogout)
         {
           message = "Logged in : " + currentUser.UserName;
           showMessage = true;
           showLogoutForm=true;
         }
        else
         {
           FormsAuthentication.SignOut();
           FormsAuthentication.RedirectToLoginPage();
         }
       }
      else
       {
        if (!isSubmitLogin) 
         {
           showLoginForm=true;
          }
        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;
            showMessage = true;
            showLoginForm = true;
          }
         }      
        }
      }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft