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

Forums / Security / Login Controls

Login Controls

14 posts, 1 answered
  1. alo
    alo avatar
    23 posts
    Registered:
    02 Jan 2009
    03 Aug 2009
    Link to this post
    I am just starting to use SiteFinity and have a couple questions regarding the built in Login Controls.  I am trying to create an approach used on telerik.com and sitefinity.com where an anonymous user would have the following
    displayed:

    Log In or Register (Why?) 

    while an authenticated user would have the following displayed:

    Welcome, FirstName LastName | Your Account | Log Out

    I'll point out that the LoginStatus control is at the beginning of the first display (Log In) and at the end of the second display (Log Out).  Also, the above example displays the first and last name while the LoginName control is set to display the User Name.

    My initial approach was to use the built in Login Controls (i.e., LoginStatus, LoginName) and the Generic Content control (to define the remaining text as links).  I placed them into a content placeholder that I have in a master page that was imported as a template.  I had to combine both of the above modes because I'm not sure how to dynamically toggle different parts on or off based on authentication.  Is it possible to control the order and which parts are displayed depending on the authentication while using the SF controls?  Or do I have to customize these controls?

    I appreciate your help,

    Al
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    03 Aug 2009
    Link to this post
    Hello alo,

    Thank you for using our services.

    I can suggest that you do the following thing. Sitefinity's login controls are not embedded into the dlls. They can be found in ~/Sitefinity/UserControls/Login/. What you can do is to modify the LoginViewControl.ascx control to display whatever you want. I have attached a sample user control that extends the LoginViewControl, you can download it and see how it is done. 

    I hope that this is what you need to do. If you have more questions, please feel free to contact us.

    Best wishes,
    Rado
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
    Answered
  3. alo
    alo avatar
    23 posts
    Registered:
    02 Jan 2009
    03 Aug 2009
    Link to this post

    I had one slight problem.  I was getting an object reference error because the code in the page load event was always trying to load the first name and last name to the labels contained in the LoggedInTemplate, which I believe isn't always there, so the find control would not return an object.

    I added a test to see if there was an authenticated user before  and now everything appears to be okay.  I used the following code to do so:

            if (HttpContext.Current.User.Identity.IsAuthenticated)  
            {  
                ProfileBase profile = ProfileBase.Create(UserManager.GetCurrentUserName());  
     
                //find user's first name from their profile  
                string firstName = profile.GetPropertyValue("FirstName"as string;  
                if (firstName != null)  
                {  
                    //find control with id Label1 nested in control LoginView1 and make it display first name  
                    ((Label)(base.FindControl("LoginView1").FindControl("FirstName"))).Text = firstName;    
                }  
                //find user's last name form their profile  
                string lastName = profile.GetPropertyValue("LastName"as string;  
                if (lastName != null)  
                {  
                    //find control with id Label2 nested in control LoginView1 and make it display last name  
                    ((Label)((base.FindControl("LoginView1").FindControl("LastName")))).Text = lastName;  
                }  
            } 

    Please let me knowif there is a better way to test if the user is authenticated.

    Thanks,

    Al
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    04 Aug 2009
    Link to this post
    Hello alo,

    Thank you for getting back to me.

    You are going in the right direction. The code you have added should work without problems. I will test it a little bit more and see if there would be any troubles.

    As I said in my previous posts if you feel that you need help regarding using/developing with Sitefinity, you are always welcome to contact us. We will be more than happy to assist you.

    Sincerely yours,
    Rado
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  5. alo
    alo avatar
    23 posts
    Registered:
    02 Jan 2009
    04 Aug 2009
    Link to this post
    Thanks for your follow up and support.  It's great to know we can count on your support team.
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Aug 2009
    Link to this post
    H,

    I am glad I was able to assist you.

    Providing quality support is part of the unique user experience that Telerik strives to deliver to our customers. If you have more questions please feel free to contact us, we will be more than happy to assist you.

    Regards,
    Rado
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  7. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    20 Aug 2009
    Link to this post
    I've placed the code on the site and received the following error message. Am I missing anything?

    Thanks,
    Pik

     

    Server Error in '/Sitefinity/Websites/Portal' Application.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 33:             {   
    Line 34:                 //find control with id Label1 nested in control LoginView1 and make it display first name   
    Line 35: ((Label)(base.FindControl("LoginView1").FindControl("FirstName"))).Text = firstName; Line 36:             }   
    Line 37:             //find user's last name form their profile   

    Source File: e:\websites\PSRinc\Sitefinity\Websites\Portal\Sitefinity\UserControls\Login\LoginViewControl.ascx.cs    Line: 35

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       UserControls_FinalLoginControls_LoginViewControl.Page_Load(Object sender, EventArgs e) in e:\websites\PSRinc\Sitefinity\Websites\Portal\Sitefinity\UserControls\Login\LoginViewControl.ascx.cs:35
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +47
       System.Web.UI.Control.AddedControl(Control control, Int32 index) +318
       System.Web.UI.ControlCollection.Add(Control child) +146
       Telerik.Cms.Web.PreviewPage.CreateChildControls() +1083
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +50
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041
    
  8. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    20 Aug 2009
    Link to this post
    Hi Pik Lee,

    Can you use the code that Albert has posted? To perform the check if the user name is null. This should fix the problem. In the original code is missing the if statement that does that check. Basically if your user is not logged in you will get the error thrown.

    All the best,
    Rado
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  9. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    20 Aug 2009
    Link to this post
    I still getting the same error message. Below are the code for the LoginViewControl.ascx and LoginViewControl.ascx.cs.  Thank you.

    LoginViewControl.ascx
        <LoggedInTemplate>
        <p>        
            <asp:Literal ID="UserLabel" runat="server" Text="Welcome, " ></asp:Literal>
            <strong><asp:Label ID="Label1" runat="server" Text="" AssociatedControlID="UserLabel"></asp:Label>
            <asp:Label ID="Label2" runat="server" Text="" AssociatedControlID="UserLabel"></asp:Label></strong> |
        </p>
        </LoggedInTemplate>

    LoginViewControl.ascx.cs
    if (HttpContext.Current.User.Identity.IsAuthenticated)   
            {   
                ProfileBase profile = ProfileBase.Create(UserManager.GetCurrentUserName());   
     
                //find user's first name from their profile   
                string firstName = profile.GetPropertyValue("FirstName") as string;
                if (firstName != null)
                {
                    //find control with id Label1 nested in control LoginView1 and make it display first name   
                    ((Label)(base.FindControl("LoginView1").FindControl("Label1"))).Text = firstName;
                }
                //find user's last name form their profile   
                string lastName = profile.GetPropertyValue("LastName") as string;   
                if (lastName != null)   
                {   
                    //find control with id Label2 nested in control LoginView1 and make it display last name   
                    ((Label)((base.FindControl("LoginView1").FindControl("Label2")))).Text = lastName;   
                }   
            }


  10. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    20 Aug 2009
    Link to this post
    I still getting the same error message. Below are the code for the LoginViewControl.ascx and LoginViewControl.ascx.cs.  Thank you.

    LoginViewControl.ascx
        <LoggedInTemplate>
        <p>        
            <asp:Literal ID="UserLabel" runat="server" Text="Welcome, " ></asp:Literal>
            <strong><asp:Label ID="Label1" runat="server" Text="" AssociatedControlID="UserLabel"></asp:Label>
            <asp:Label ID="Label2" runat="server" Text="" AssociatedControlID="UserLabel"></asp:Label></strong> |
        </p>
        </LoggedInTemplate>

    LoginViewControl.ascx.cs
    if (HttpContext.Current.User.Identity.IsAuthenticated)   
            {   
                ProfileBase profile = ProfileBase.Create(UserManager.GetCurrentUserName());   
     
                //find user's first name from their profile   
                string firstName = profile.GetPropertyValue("FirstName") as string;
                if (firstName != null)
                {
                    //find control with id Label1 nested in control LoginView1 and make it display first name   
                    ((Label)(base.FindControl("LoginView1").FindControl("Label1"))).Text = firstName;
                }
                //find user's last name form their profile   
                string lastName = profile.GetPropertyValue("LastName") as string;   
                if (lastName != null)   
                {   
                    //find control with id Label2 nested in control LoginView1 and make it display last name   
                    ((Label)((base.FindControl("LoginView1").FindControl("Label2")))).Text = lastName;   
                }   
            }


  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Aug 2009
    Link to this post
    Hi Pik Lee,

    The error is thrown because control with ID first name cannot be found in LoggedInTemplate. However below is another solution that you can use.

    <asp:LoginView ID="LoginView1" runat="server"
        <LoggedInTemplate> 
        <p> 
            <asp:Label ID="Fname" runat="server" /> 
            <asp:Label ID="Lname" runat="server" /> 
            <asp:Literal ID="UserLabel" runat="server" Text="Logged as: " ></asp:Literal> 
            <strong><asp:LoginName ID="LoginName" runat="server" /></strong
            ( <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutPageUrl="~/Default.aspx" /> ) 
        </p> 
        </LoggedInTemplate> 
        <AnonymousTemplate> 
            <p> 
                <asp:LoginStatus ID="LoginStatus2" runat="server" /> 
            </p> 
        </AnonymousTemplate> 
    </asp:LoginView> 

    protected override void OnLoad(EventArgs e) 
        { 
            base.OnLoad(e); 
            Label fNameLabel = (Label)LoginView1.FindControl("Fname"); 
            Label lName = (Label)LoginView1.FindControl("Lname"); 
            if (fNameLabel != null && lName != null
            { 
                fNameLabel.Text = "a"
                lName.Text = "b"
            } 
        } 


    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  12. Pik Lee
    Pik Lee avatar
    142 posts
    Registered:
    13 Aug 2012
    20 Aug 2009
    Link to this post
    Thanks Ivan. It is working now.
  13. Laura
    Laura avatar
    311 posts
    Registered:
    25 Feb 2008
    14 Jan 2010
    Link to this post
    Quick question - on this page for the LoginView control, there are two similar examples -  
    however one uses:
    protected override void OnLoad(EventArgs e) 
        { 
            base.OnLoad(e);

    and the other uses

    protected

     

    void Page_Load(object sender, EventArgs e)

     

     

    Is there a better than other way? What is best practice?

    Thank you. 

     

  14. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    14 Jan 2010
    Link to this post
    Hello Laura,

    Control.Load Event occurs when the server control is loaded into the Page object.
    Control.OnLoad Method raises the Load event( the above one).

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
14 posts, 1 answered