Programmatic Login - General Discussions - General Discussions - Progress Community
 General Discussions

Programmatic Login

  • Programmatic Login
  • Is there a way to programmatically login in beta 2?  I've tried the usual methods with no luck so far.  I'm new to SF so maybe I'm missing something obvious.

    Thanks,
    Matt

  • Hello Matt,

    We generate our own authentication cookie. You should use SecurityManager.SetAuthenticationCookie method to authenticate an user programmatically.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • Thanks, that did the trick.
    For anyone else looking to do this, here are a couple of specific examples, either of which should work:

    SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, user.ProviderName, user, false);

    SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, Membership.Provider.Name, username, false);

  • Matt,

    Thanks for posting the two lines of code. I tried both. It authenticates ok, but when I get to the DestinationPage it shows me as Anonymous. Did you have the same issue?

    1 - I tried:
    SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, Membership.Provider.Name, username, False)

    2 - I tried:
    Dim usr As Model.User = DirectCast(Membership.GetUser(username), Model.User)
    SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, usr.ProviderName, usr, False)

    Many thanks,
    Andrei
  • Andrei,

    Here's the code that's working for me to login after new user registration:

    UserManager userMan = new UserManager();
    user = userMan.GetUser(username);
    string membershipProvider = user.ProviderName;
    HttpResponseBase response = SystemManager.CurrentHttpContext.Response;
    SecurityManager.SetAuthenticationCookie(response, membershipProvider, user, false);
    Response.Redirect("~/secure/index");

    This works for me in this one place, but I'm getting into trouble trying to use it for return user login.  I saw your post in another thread.  If you check the user immediately after setting the auth cookie, I don't think it'll reflect the new user yet. The cookie has to return to the browser and come back in the next request before SF will recognize the identity of the logged in user.  Seems like it should work after the redirect though...

    Sorry for the delayed response... did you already get past this issue?

    Matt


  • Matt,

    I agree that "it should work after the redirect" but why it does not, I have no idea. Thanks for the code, but I get the same thing. It gets me through to the target page, but still shows 'Anonymous', which is most frustrating. It seems that Sitefinity is not that compatible with the ASP Login control after all. Everything I have tried so far with the <asp:Login control so far has proved to be a waste of time (loads of time).

    So I switched back to the Sitefinity LoginForm control. The first problems I started having was that it would throw Unhandled exceptions. Then I found out that it requires a ScriptManager and the template provided by Sitefinity I was using did not have the ScriptManager. So I created my own template with a ScriptManager inside and used it. The LoginForm worked fine, but all the styling disappeared. So now I am trying to style the LoginForm with relatively low success. I try saying DisplayRememberMe="False" but the front end does not reflect. All I want is for it to stay with its default style, but at runtime it loses that.

    Also the Edit option has disappeared when dropping the LoginForm control on to the page for unknown reasons. Now I cannot edit it or delete it. Good times...

    Many thanks again,
    Andrei
  • Andrei,

    I'm still working on our login form, and running up against some of the same issues... also went back to just using the LoginForm control just until I have time to come back to it.  I'll keep you posted if I come up with anything new, because I would really like to get away from this control and use my own form.  Who knows, maybe all our problems will be solved with the RC release today.

    Matt

  • Andrei,

    Here's my approach for controlling the styles of the LoginForm and for hiding the "remember me" checkbox, as well as the default title that I couldn't seem to override (you can see where I tried in the control attributes).  Kind of a hack, but it works.

    Matt

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Login.ascx.cs" Inherits="SitefinityWebApp.Custom.Widgets.Login" %>
    <%@ Register TagPrefix="t" Namespace="Telerik.Sitefinity.Security.Web.UI" Assembly="Telerik.Sitefinity" %>

    <style type="text/css">
    div.login ol li
    list-style-type:none;
    div.login h2
    display:none;
    div.login ol li label
    width:200px;
    div.login li.sfCheckBoxWrapper
    display:none;
    </style>

    <h1>Login</h1>

    <div class="login">
    <t:LoginForm runat="server" ID="loginForm1"
    Title="Login" TitleText="Login"
    ShowChangePasswordLink="false" 
    ShowForgotPasswordLink="false"
    ShowHelpLink="false"
    ShowRegisterUserLink="false"
    DisplayRememberMe="false"
    />
    </div>

  • Hi Matt, Andrei

    Below is a sample code that illustrates how to log in a user .


    public partial class WebPageTest1 : System.Web.UI.Page
       
           protected void Page_Load(object sender, EventArgs e)
           
               // get the login button
               var b = this.LoginControl.FindControl("LoginButton") as Button;
               // subscribe for click event
               b.Click += new EventHandler(b_Click);
               // get the current user
               var user = SecurityManager.GetCurrentUser();
               if (user != null)
               
                   // get the identity
                   var ident = user.Identity.Name;
                   // force log out for this stest
                   SecurityManager.Logout();
               
           
           void b_Click(object sender, EventArgs e)
           
               // Login the user.
               var now = DateTime.UtcNow;
               var ip = SystemManager.CurrentHttpContext.Request.UserHostAddress;
               var manager = UserManager.GetManager();
               var user = manager.GetUser("admin");
               user.IsLoggedIn = true;
               user.LastLoginIp = ip;
               user.LastLoginDate = now;
               user.LastActivityDate = now;
               SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, "Default", user, true);
               manager.Provider.SuppressSecurityChecks = true;
               manager.SaveChanges();
               manager.Provider.SuppressSecurityChecks = false;
               Response.Redirect("~/WebPageTest1.aspx");
     
           
       


    You have to use the method
               SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, "Default", user, true);

    Where the last parameter here indicates whether "Rembember Me" CheckBox has been checked. In the sample above I am setting this value explicitly. 

    After the user has been authenticated this will call Page_Load again, because of the Response.Redirect and the user identity is "admin", so you will not get "Anonymous" user anymore.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • Ivan saves the day. Just tried it and it works. Thank you very much.
    Matt many thanks again, and yes I am looking forward to the webinar (20 more minutes).

    Andrei

  • Hi guys,

    I want to do the same, but I'm using Sitefinity 3.7 with .Net3.5 and using c#.

    Could you guys help me on  how to login a user programmaticly with c# and my sitefinity version?
    Struggling a bit with this...

    I did try with the code posted above, but I don't think all those methods is available in my version, can't seem to find them.

    Thanks in advanced!
  • This is what I've got, but not sure if I'm on the right track here...

    UserManager manager = new UserManager();
    System.Web.Security.MembershipUser user = manager.GetUser( "username" );
    if( manager.ValidateUser( "username", "password" ) )
    user.LastLoginDate = DateTime.Now;
    manager.SetAuthenticationCookie( new HttpCookie( "sitefinity" ) );


    I'm not sure what should be in the cookie, I also saw there is no property for the user where you can set "IsLoggedIn" to true...so not sure how I should go about this.
  • Hello Gerrit,

    In Sitefinity 3.x you should use FormsAuthentication cookie

    HttpCookie cookie = this.Response.Cookies[FormsAuthentication.FormsCookieName];
    UserManager.Default.SetAuthenticationCookie(cookie);


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • Hi Ivan,

    Thank you for your quick reply.

    Oky I see, just one thing, I'm getting this error now :

    "Invalid value for 'encryptedTicket' parameter." 

    This happens when I call this line : "UserManager.Default.SetAuthenticationCookie( cookie );"
    I assume I must set a value in the cookie called "encryptedTicket".
    Where would I get that value?

    Thank you for your help!