You are here: Ektron Namespace > .NET Assemblies > Ektron.Cms.API Namespace > Ektron.Cms.API.User Namespace > Classes > User Class > User Methods > LogInUser Method > LogInUser
Ektron CMS400.NET API Documentation
ContentsIndexHome
Example

The following example shows how to create a Web page that programmatically logs a user into CMS400.NET. Please note that the code-behind in this example does not login a new user if someone is already logged into the Workarea. This example uses some standard drag and drop controls and a small section of VB code utilizing the LogInUser method. This method uses the InternalAdmin to log the user into CMS400.NET. In the first section, we will deal with the standard drag and drop controls. In the second, we'll deal with the code behind. 

 

  1. Between the form tags, add a label to show where the Username is entered.
       <asp:Label ID="Label6" runat="server" Text="Username: " Width="76px"></asp:Label>
  2. Add a text box to enter the Username.
       <asp:TextBox ID="txtUserName" runat="server" Width="134px"></asp:TextBox>
  3. Add a RequiredFieldValidator control from the Validation control menu. This validates that something appears in the Username box.
       <asp:RequiredFieldValidator ID="rfvUserName" runat="server" ErrorMessage="Please enter user name"
            ControlToValidate="txtUserName">Please enter user name</asp:RequiredFieldValidator>
  4. Add two breaks for spacing purposes.
       <br /><br />
  5. Add a label to show where the Password is entered.
       <asp:Label ID="Label8" runat="server" Text="Password: " Width="76px"></asp:Label>
  6. Add a text box to enter the Password.
       <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="134px"></asp:TextBox>
  7. Add a RequiredFieldValidator control from the Validation control menu. This validates that something appears in the password box.
       <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ErrorMessage="Please enter password"
           ControlToValidate="txtPassword">Please enter password</asp:RequiredFieldValidator>
  8. Add two breaks for spacing purposes.
       <br /><br />
  9. Add a button to submit the user's login information. We will add a button click event in the code behind.
       <asp:Button ID="btnLogin" runat="server" Text="Login" Width="57px" />
  10. Add two last breaks for spacing purposes.
       <br /><br />
  11. Add a results label to display a message that states whether the user is logged in.
       <asp:Label ID="lblResult" runat="server" Width="432px"></asp:Label>

 

Add the following information to the code behind of your aspx.vb page.

  1. Add a page load event that checks to see if a user is logged into CMS400.NET.

       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
           Dim api As New Ektron.Cms.CommonApi
           If (api.RequestInformationRef.CallerId > 0) Then
               lblResult.Text = api.RequestInformationRef.LoggedInUsername & " is logged in." & <"br/>"
           End If
       End Sub

  1. Add a button click event.

       Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click

  1. Create an object for the user API.

           Dim userAPI As New Ektron.Cms.API.User.User

  1. Create an object for the common API.

           Dim api As New Ektron.Cms.CommonApi

  1. Create an object to store the user data.

           Dim userObj As UserData

  1. Create a Try/Catch around the LogInUser method. This allows you to catch any exceptions and helps when debugging.
  2. Check to see if the user is already logged in.

           Try
               If (api.RequestInformationRef.CallerId > 0) Then
                   lblResult.Text = api.RequestInformationRef.LoggedInUsername & " is already logged in."

  1. If the user is not logged in, proceed with the login.

               Else
                   userObj = userAPI.LogInUser(txtUserName.Text.Trim, txtPassword.Text.Trim, "localhost", "", "")   

  1. Check to see whether the login was successful. If it was unsuccessful, display a message.

                   If userObj.Id = 0 Then
                       lblResult.Text = "Login failed."
                       Exit Sub
                   End If 

  1. Set the authentication cookie and Response.Redirect.

                   userAPI.SetAuthenticationCookie(userObj)
                   Response.Redirect(Request.Url.ToString)
               End If

  1. If there is an error, let the user know what happened.

            Catch ex As Exception
               lblResult.Text = ex.Message
            End Try 
        End Sub
Created with a commercial version of Doc-O-Matic. In order to make this message disappear you need to register this software. If you have problems registering this software please contact us at [email protected].
Copyright (c) 2008. All rights reserved.