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

Forums / General Discussions / Programmatic Login

Programmatic Login

38 posts, 1 answered
  1. David Whritenour
    David Whritenour avatar
    22 posts
    Registered:
    30 Nov 2009
    20 Apr 2012
    Link to this post
    Hi Miles -

    Did you ever find a solution to this?
    Having the same exact issue.

    David
  2. Miles
    Miles avatar
    32 posts
    Registered:
    23 Nov 2011
    26 Apr 2012
    Link to this post
    Yes I was but I'm not 100% how correct this is - I'm never 100% when working with Sitefinity :P

    01.protected void linkButtonLogin_Click(object sender, EventArgs e)
    02.{
    03.    var manager = UserManager.GetManager();
    04.    string userName = textboxUsername.Text.Trim();
    05.    string password = textboxPassword.Text.Trim();
    06. 
    07.    if (manager.ValidateUser(userName, password))
    08.    {
    09.        DateTime now = DateTime.UtcNow;
    10.        var user = manager.GetUser(userName);
    11.        user.IsLoggedIn = true;
    12.        user.LastLoginIp = SystemManager.CurrentHttpContext.Request.UserHostAddress;
    13.        user.LastLoginDate = now;
    14.        user.LastActivityDate = now;
    15.        var loginReason = SecurityManager.AuthenticateUser(UserManager.GetDefaultProviderName(), userName, password, true);
    16. 
    17.        if (loginReason == UserLoggingReason.UserAlreadyLoggedIn)
    18.        {
    19.            SecurityManager.Logout(UserManager.GetDefaultProviderName(), user.Id);
    20.            loginReason = SecurityManager.AuthenticateUser(UserManager.GetDefaultProviderName(), userName, password, true);
    21.        }
    22. 
    23.        if (loginReason == UserLoggingReason.Success)
    24.        {
    25.            manager.Provider.SuppressSecurityChecks = true;
    26.            manager.SaveChanges();
    27. 
    28.            FormsAuthentication.SetAuthCookie(userName, true);
    29. 
    30.            if (Request["returnUrl"] == null)
    31.                Response.Redirect(String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority));
    32.            else
    33.                Response.Redirect(Request["returnUrl"]);
    34.            //
    35.        }
    36.        else
    37.            literalErrorMessage.Text = loginReason.ToString();
    38.        //
    39.    }
    40.    else
    41.        literalErrorMessage.Text = "Invalid username or password";
    42.    //
    43.}

  3. David Whritenour
    David Whritenour avatar
    22 posts
    Registered:
    30 Nov 2009
    27 Apr 2012
    Link to this post
    Thanks Miles - we give this a try today!

    David
  4. Patrick
    Patrick avatar
    18 posts
    Registered:
    31 Mar 2015
    24 Apr 2015 in reply to Ivan Dimitrov
    Link to this post
    Hello. I'm new to Sitefinity and so I decided to learn about it using a sample app from the SDK. I'm using the Telerik.Sitefinity.Samples.Quantum app. I imported it in my Visual Studio and added a web form to the project (ShowClaims.aspx) which is a page that will simply display the identity claims contained in the security token returned to the app by the STS. Here's my question: How does one go about adding a page to the app from the front end UI? I'm currently logged I the dashboard (http://localhost:60876/Sitefinity/dashboard) which is what I call the front end UI, and I assume that would be the way to add my new page to the app. I see where I can create a new page but where do I point it to the code I entered in Studio?
  5. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    02 Dec 2016
    29 Apr 2015
    Link to this post
    Hello Patrick,

    If you have created the .aspx page in the root of your project folder in Visual Studio, you can access it like the following: http://yourdomain.com/ShowClaims.aspx.

    Another option will be to create a user control (.ascx file) and place your logic there and build the solution. Then you can register the user control in the Toolbox using Thunder as described in the following article.

    After you register the widget, please restart the application and login to the Sitefinity backend, click on the Pages tab and create a new page or open an existing page in edit mode.

    When you open a page in edit mode you will see the Toolbox section on the right hand side of the page. This is the section where you can drag and drop the built-in widgets which come with Sitefinity and your custom widgets as well. You should see your custom widget there under the section where you have registered it. After you find your custom widget, please drop it on the page.

    If you would like to add a code behind (the aspx.cs file created in Visual Studio) to a Sitefinity page created through the Sitefinity backend, you can checkout the following blog post for more details on this.

    In addition to this, since this forum thread is opened to discuss how to login programatically in Sitefinity, I would kindly ask you to open a new thread or open a support ticket if you have any further questions about creating pages or creating widgets in Sitefinity. Thank you for your kind understanding.

    Regards,
    Sabrie Nedzhip
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  6. Vladimir
    Vladimir avatar
    13 posts
    Registered:
    30 Apr 2015
    11 May 2015
    Link to this post

    Hi everyone!

     I have a question related to Programmatic Login. I have a situation in which I should login a user based on 3 fields(ID, username, password). The login process is made against an external REST web API.

    I tried to implement my own login widget, with these 3 fields, but I could not set authentication cookie, since SecurityManager.SetAuthenticationCookie  is internal - no luck(.

    I tried to implement my own custom MembershipProvider, but  SecurityManager.AuthenticateUser(UserManager.GetDefaultProviderName(), userName, password, true); needs only 2 parameters (username and password).

    Actually, my question is: Is there a way to implement a login functionality in Sitefinity based on more than just 2 fields (username and password);

    Many thanks!
  7. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    02 Dec 2016
    14 May 2015
    Link to this post
    Hello Vladimir,

    Please note that you should use the overloads of the SecurityManager.AuthenticateUser() method to authenticate the users in Sitefinity.

    If you would like to allow users to enter their id when they submit their credentials, before the login you can use the API to get the user who has this id and/or the entered username and then log this user:

    UserManager userManager = UserManager.GetManager();
     
    User user = // get the user here by username or/and by the entered id
      
    UserLoggingReason result = SecurityManager.AuthenticateUser(null, "username", true, out user);

    Please also refer to this article for more details about authenticating a user in Sitefinty.

    Regards,
    Sabrie Nedzhip
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  8. Vladimir
    Vladimir avatar
    13 posts
    Registered:
    30 Apr 2015
    15 May 2015 in reply to Sabrie Nedzhip
    Link to this post

    Hi Sabrie!

     That's great, I didn't noticed that overload of the method. BUT, it returns also a USER(as out parameter). What it actually does - calls my custom membership method GetUser(string username). Here comes the problem. I could not call my API only with username parameter. An access token should also be provided. So my membership provider could not access my web API to get a user.

     I tried a workaround. Created a method SetToken(string token) to membership provider. Than, when I GetUser(string) is called by SecurityManager.AuthenticateUser(string,string,bool,out User), I try to get the User.

    I'm not quite sure if it is a good implementation. Is membership implementation is Singleton throughout Sitefinty?(because other users will also supply their tokens to custom provider)

    Do you have any ideas?

    Many thanks!
38 posts, 1 answered
1 2