How to Login automatically in Sitefinity through API

Posted by Community Admin on 04-Aug-2018 15:46

How to Login automatically in Sitefinity through API

All Replies

Posted by Community Admin on 05-Sep-2012 00:00

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

Posted by Community Admin on 05-Sep-2012 00:00

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.

Posted by Community Admin on 07-Sep-2012 00:00

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

This thread is closed