1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to Login automatically in Sitefinity through API

How to Login automatically in Sitefinity through API

3 posts, 0 answered
  1. Mike
    Mike avatar
    11 posts
    Registered:
    03 May 2012
    05 Sep 2012
    Link to this post
    Hi,

    Our site has a custom registration page, which works fine, and redirects users to a thank you page.

    We need this to automatically log users in. 

    Here's the registration code:
    private void CreateAndRedirect(bool isValidatedMember)
    {
        var result = this.CreateUser(
            this.Email.Text,
            this.Password.Text,
            this.FirstName.Text,
            this.LastName.Text,
            this.Email.Text,
            string.Empty,
            string.Empty,
            true,
            this.Number.Text,
            this.Mail.Checked ? "1" : "0",
            this.Country.Text,
            isValidatedMember,
            this.birthdayDay.Text,
            this.birthdayMonth.SelectedIndex.ToString(CultureInfo.CurrentUICulture),
            this.birthdayYear.Text);
     
     
     
        if (result == MembershipCreateStatus.Success)
        {
            if (isValidatedMember)
            {
                this.Response.Redirect(this.ResolveUrl("~/members/official-welcome"));
            }else{
                this.Response.Redirect(this.ResolveUrl("~/members/welcome"));
            }
        }
    }


    Please can you suggest how we can do this?

    Thanks,

    Andy
  2. Mark
    Mark avatar
    147 posts
    Registered:
    01 Jun 2010
    05 Sep 2012
    Link to this post
    The following is working for me:

    var validate = SecurityManager.AuthenticateUser("Default", Username.Text, Password.Text, true);
    bool authenticated = (validate == UserLoggingReason.Success);

    I noticed you weren't setting a nickname when creating the user. You might want to do that, it's caught up with me before.
  3. David
    David avatar
    38 posts
    Registered:
    05 Apr 2012
    07 Sep 2012
    Link to this post
    Hi Mark

    I am another developer on the project with Andy. 

    We are setting the nickname inside the CreateUser function.

    When we use the SecurityManager.AuthenticateUser the result comes back as 'Success' but when we redirect to another page via Response.Redirect, the successful authentication seems to have been lost.

    Some pseudo code to out line what we are seeing:
    1.var userManager = UserManager.GetManager();
    2.SecurityManager.AuthenticateUser(userManager.Provider.Name, Email.Text, Password.Text, true, out user);
    3.//Request.IsAuthenticated is true here
    4.Response.Redirect("/welcome");
    5.//
    6.//Welcome page
    7.//Response.Redirect = false;

    The simplest case I have is a brand new Sitefinity site with one page that has a Login/Logout widget on it. 
    I have a seperate aspx page with a button and a label. Button click calls lines 1 and 2 above and displays the result (which is success).
    Navigate to the sitefinity page and see the login/logout widget showing 'Log in' when I was expecting it to show 'log out'.

    The site is Sitefinity 5.1 using Claims based authentication.

    Any suggestions?

    Cheers,
    David Bigg
3 posts, 0 answered