Answers.Body: <p>If you want to login a user, you need to use the <code>login</code> function</p>
  
  <pre><code>craft()-&gt;userSession-&gt;login($loginName, $password, $rememberMe))
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-04T15:47:27.243Z
Answers.Text: Is the $rememberMe parameter optional, and is there an expiration on it?
Answers.UserDisplayName: buck1112
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-15T06:59:38.560Z
Answers.Text: @Robin How to do this in Craft 3, please?
Answers.UserDisplayName: Ra3d K
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-03T21:13:30.373Z
Answers.LastActivityDate: 2018-10-03T21:13:30.373Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I'm writing an LDAP authentication plugin, which, if the authentication matches, will log the user into a 'general' username/password, in order to view a protected page.  The 'general' username/password is part of a usergroup with restricted priveleges. </p>
  
  <p>In my plugin, in the controller file, I have this:</p>
  
  <pre><code>craft()-&gt;userSession-&gt;login($username, $password);
  </code></pre>
  
  <p>with the $username and $password variables set just above that.  However, the login process does not appear to be happening, as my check in my template is not displaying the username:</p>
  
  <pre><code>{% if currentUser %}
  Welcome, {{ currentUser.friendlyName }}!
  {% endif %}
  </code></pre>
  
  <p>However, when I log on as that 'general' username/password, from the login page, and then refresh my template, it does show that the login for that profile has successfully occurred.  </p>
  
  <p>What may need to be done to effect a successful login, in the plugin?<br>
  Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-04T15:46:02.720Z
Comments.Text: My error - I corrected the code above to show that, indeed, I do have craft()->userSession->login($username, $password)
Comments.UserDisplayName: buck1112
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-05T18:34:08.327Z
Comments.Text: This is in Craft 2, right? How exactly are you triggering the controller action?
Comments.UserDisplayName: Ben Croker
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-05T19:29:01.510Z
Comments.Text: Yes it is, thanks.  I am triggering the controller action via the input type="hidden" name="action" value=myPluginName/myClassName/myFunctionName.
Comments.UserDisplayName: buck1112
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-05T19:33:17.680Z
Comments.Text: The really odd thing is that it is working today, without any changes being made today.  I wonder if it's a caching issue.  One other common 'issue' is that when I re-login (as the same user), I get a "the csrf token could not be verified".  When I hit the <enter> key with the url, the form loads correctly (without the error message).  Beyond that little quirk, it seems to be working ok.  I'd still like to trap that error somehow, though.
Comments.UserDisplayName: buck1112
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-05T19:33:48.503Z
Comments.Text: To clarify my last post, by "working," I mean that the plugin is logging me in as the test user.
Comments.UserDisplayName: buck1112
Tags: <plugin-development><login><craft2>
Title: Programmatically Login From Plugin
ThreadText: # Programmatically Login From Plugin
  
  (Asked by: username_0 on 2018-10-03)
  
  I'm writing an LDAP authentication plugin, which, if the authentication matches, will log the user into a 'general' username/password, in order to view a protected page. The 'general' username/password is part of a usergroup with restricted priveleges.
  
  In my plugin, in the controller file, I have this:
  
  ```
  craft()->userSession->login($username, $password);
  
  ```
  
  with the $username and $password variables set just above that. However, the login process does not appear to be happening, as my check in my template is not displaying the username:
  
  ```
  {% if currentUser %}
  Welcome, {{ currentUser.friendlyName }}!
  {% endif %}
  
  ```
  
  However, when I log on as that 'general' username/password, from the login page, and then refresh my template, it does show that the login for that profile has successfully occurred.
  
  What may need to be done to effect a successful login, in the plugin?  
  Thanks!
  
  **Tags:** plugin-development, login, craft2
  
  > **Comments:**
  > **username_0**: My error - I corrected the code above to show that, indeed, I do have craft()->userSession->login($username, $password)  
  > **username_1**: This is in Craft 2, right? How exactly are you triggering the controller action?  
  > **username_0**: Yes it is, thanks.  I am triggering the controller action via the input type="hidden" name="action" value=myPluginName/myClassName/myFunctionName.  
  > **username_0**: The really odd thing is that it is working today, without any changes being made today.  I wonder if it's a caching issue.  One other common 'issue' is that when I re-login (as the same user), I get a "the csrf token could not be verified".  When I hit the <enter> key with the url, the form loads correctly (without the error message).  Beyond that little quirk, it seems to be working ok.  I'd still like to trap that error somehow, though.  
  > **username_0**: To clarify my last post, by "working," I mean that the plugin is logging me in as the test user.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-10-03. Score: 2
  
  If you want to login a user, you need to use the `login` function
  
  ```
  craft()->userSession->login($loginName, $password, $rememberMe))
  
  ```
  
  > **Comments:**
  > **username_0**: Is the $rememberMe parameter optional, and is there an expiration on it?  
  > **username_3**: @username_2 How to do this in Craft 3, please?  
  
  ---
  
  