How to programmatically login an admin user?How to perform auto-login in Magento2 admin?Login to multiple magento admin panelHow to force a admin user to change their password?Can't login to Magento admin after migrationAdmin Not Redirecting After LoginHow to log in an admin user programmatically from frontend controller in Magento 2How to perform auto-login in Magento2 admin?Testing magento 2 admin user _isAllowedWhy are my images being loaded as the Admin Login page?Backend admin login programmaticallyNot having the page reload or being redirected if the login credentials are wrong - Magento 2.2

How did old MS-DOS games utilize various graphic cards?

I have a problem assistant manager, but I can't fire him

How to produce a more sophisticated pie chart?

Certain search in list

SQL counting distinct over partition

What to do when surprise and a high initiative roll conflict with the narrative?

What makes Ada the language of choice for the ISS's safety-critical systems?

How to manually rewind film?

Electricity free spaceship

Active low-pass filters --- good to what frequencies?

Implement Own Vector Class in C++

Does the Long March-11 increase its thrust after clearing the launch tower?

How can this tool find out registered domains from an IP?

Getting UPS Power from One Room to Another

A IP can traceroute to it, but can not ping

Group Integers by Originality

Does a scale have more than seven chords?

How to tell your grandparent to not come to fetch you with their car?

What is the `some` keyword in SwiftUI?

Is using haveibeenpwned to validate password strength rational?

English word for "product of tinkering"

is it possible for a vehicle to be manufactured witout a catalitic converter

Why can my keyboard only digest 6 keypresses at a time?

How to use memset in c++?



How to programmatically login an admin user?


How to perform auto-login in Magento2 admin?Login to multiple magento admin panelHow to force a admin user to change their password?Can't login to Magento admin after migrationAdmin Not Redirecting After LoginHow to log in an admin user programmatically from frontend controller in Magento 2How to perform auto-login in Magento2 admin?Testing magento 2 admin user _isAllowedWhy are my images being loaded as the Admin Login page?Backend admin login programmaticallyNot having the page reload or being redirected if the login credentials are wrong - Magento 2.2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am on Magento 2.3.1 and I am trying to login an admin user automatically given their username. I have tried using the MagentoBackendModelAuthSession class like this:



$this->_authSession->setUser($adminUserName);
$this->_authSession->processLogin();
$this->_authSession->refreshAcl();
$this->_authSession->prolong();


and then redirecting like this:



return $this->_backendUrl->getUrl('admin/dashboard/index');


but it keeps giving me the login prompt instead of the dashboard even though this test:



var_dump($this->_authSession->isLoggedIn());


returns the boolean True.



Any help would be appreciated










share|improve this question







New contributor



user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • want to login in admin panel programmaticlly?

    – Asad Khan
    May 31 at 11:35











  • yes, I am trying to automatically log into the admin panel

    – user79886
    May 31 at 11:40

















0















I am on Magento 2.3.1 and I am trying to login an admin user automatically given their username. I have tried using the MagentoBackendModelAuthSession class like this:



$this->_authSession->setUser($adminUserName);
$this->_authSession->processLogin();
$this->_authSession->refreshAcl();
$this->_authSession->prolong();


and then redirecting like this:



return $this->_backendUrl->getUrl('admin/dashboard/index');


but it keeps giving me the login prompt instead of the dashboard even though this test:



var_dump($this->_authSession->isLoggedIn());


returns the boolean True.



Any help would be appreciated










share|improve this question







New contributor



user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • want to login in admin panel programmaticlly?

    – Asad Khan
    May 31 at 11:35











  • yes, I am trying to automatically log into the admin panel

    – user79886
    May 31 at 11:40













0












0








0








I am on Magento 2.3.1 and I am trying to login an admin user automatically given their username. I have tried using the MagentoBackendModelAuthSession class like this:



$this->_authSession->setUser($adminUserName);
$this->_authSession->processLogin();
$this->_authSession->refreshAcl();
$this->_authSession->prolong();


and then redirecting like this:



return $this->_backendUrl->getUrl('admin/dashboard/index');


but it keeps giving me the login prompt instead of the dashboard even though this test:



var_dump($this->_authSession->isLoggedIn());


returns the boolean True.



Any help would be appreciated










share|improve this question







New contributor



user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am on Magento 2.3.1 and I am trying to login an admin user automatically given their username. I have tried using the MagentoBackendModelAuthSession class like this:



$this->_authSession->setUser($adminUserName);
$this->_authSession->processLogin();
$this->_authSession->refreshAcl();
$this->_authSession->prolong();


and then redirecting like this:



return $this->_backendUrl->getUrl('admin/dashboard/index');


but it keeps giving me the login prompt instead of the dashboard even though this test:



var_dump($this->_authSession->isLoggedIn());


returns the boolean True.



Any help would be appreciated







magento2 admin magento2.3 admin-login admin-user






share|improve this question







New contributor



user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question







New contributor



user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question






New contributor



user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked May 31 at 11:09









user79886user79886

52




52




New contributor



user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




user79886 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • want to login in admin panel programmaticlly?

    – Asad Khan
    May 31 at 11:35











  • yes, I am trying to automatically log into the admin panel

    – user79886
    May 31 at 11:40

















  • want to login in admin panel programmaticlly?

    – Asad Khan
    May 31 at 11:35











  • yes, I am trying to automatically log into the admin panel

    – user79886
    May 31 at 11:40
















want to login in admin panel programmaticlly?

– Asad Khan
May 31 at 11:35





want to login in admin panel programmaticlly?

– Asad Khan
May 31 at 11:35













yes, I am trying to automatically log into the admin panel

– user79886
May 31 at 11:40





yes, I am trying to automatically log into the admin panel

– user79886
May 31 at 11:40










1 Answer
1






active

oldest

votes


















0














This is what i managed to do (based on How to perform auto-login in Magento2 admin?) and is working



?php
require __DIR__ . '/app/bootstrap.php';

class TestApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface

public function __construct(
MagentoFrameworkObjectManagerInterface $objectManager
)

$this->_objectManager = $objectManager;
$this->_eventManager = $this->_objectManager->get('MagentoFrameworkEventManager');
$this->_areaList = $this->_objectManager->get('MagentoFrameworkAppAreaList');
$this->_request = $this->_objectManager->get('MagentoFrameworkAppRequestHttp');
$this->_response = $this->_objectManager->get('MagentoFrameworkAppResponseHttp');
$this->_configLoader = $this->_objectManager->get('MagentoFrameworkObjectManagerConfigLoaderInterface');
$this->_state = $this->_objectManager->get('MagentoFrameworkAppState');
$this->_filesystem = $this->_objectManager->get('MagentoFrameworkFilesystem');
$this->registry = $this->_objectManager->get('MagentoFrameworkRegistry');

parent::__construct($objectManager, $this->_eventManager, $this->_areaList, $this->_request, $this->_response, $this->_configLoader, $this->_state, $this->_filesystem, $this->registry);




public function launch()

$areaCode = 'adminhtml';
$username = 'superadmin3';

$this->_state->setAreaCode($areaCode);

$this->_request->setPathInfo('/admin');

$this->_objectManager->configure($this->_configLoader->load($areaCode));

/** @var MagentoUserModelUser $user */
$user = $this->_objectManager->get('MagentoUserModelUser')->loadByUsername($username);

/** @var MagentoBackendModelAuthSession $session */
$session = $this->_objectManager->get('MagentoBackendModelAuthSession');
$session->setUser($user);
$session->processLogin();

if ($session->isLoggedIn())
$cookieManager = $this->_objectManager->get('MagentoFrameworkStdlibCookieManagerInterface');
$cookieValue = $session->getSessionId();
if ($cookieValue)
$sessionConfig = $this->_objectManager->get('MagentoBackendModelSessionAdminConfig');
$cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
$cookieMetadata = $this->_objectManager->get('MagentoFrameworkStdlibCookieCookieMetadataFactory')
->createPublicCookieMetadata()
->setDuration(3600)
->setPath($cookiePath)
->setDomain($sessionConfig->getCookieDomain())
->setSecure($sessionConfig->getCookieSecure())
->setHttpOnly($sessionConfig->getCookieHttpOnly());
$cookieManager->setPublicCookie($sessionConfig->getName(), $cookieValue, $cookieMetadata);
/** @var MagentoSecurityModelAdminSessionsManager $adminSessionManager */
$adminSessionManager = $this->_objectManager->get('MagentoSecurityModelAdminSessionsManager');
$adminSessionManager->processLogin();



/** @var MagentoBackendModelUrlInterface $backendUrl */
$backendUrl = $this->_objectManager->get('MagentoBackendModelUrlInterface');
$path = $backendUrl->getStartupPageUrl();
$url = $backendUrl->getUrl($path);
$url = str_replace('autologin.php', 'index.php', $url);
header('Location: '.$url);
exit;

return $this->_response;



$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);





share|improve this answer

























  • Thanks ! It works ! However, in magento 2.3.1 an instance of the class MagentoBackendModelSessionAdminConfig must be used in setPublicCookie to get the correct session name. Otherwise, the cookie is named PHPSESSID and the redirect will prompt the admin panel login page

    – user79886
    May 31 at 11:59












  • if its works for you then click on the tick mark to help other people as well and also upvote as well. thanks

    – Asad Khan
    May 31 at 12:02












  • Yeah, just waiting for the edit to get accepted so it works for others as well. Thanks !

    – user79886
    May 31 at 12:07











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






user79886 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f276861%2fhow-to-programmatically-login-an-admin-user%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This is what i managed to do (based on How to perform auto-login in Magento2 admin?) and is working



?php
require __DIR__ . '/app/bootstrap.php';

class TestApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface

public function __construct(
MagentoFrameworkObjectManagerInterface $objectManager
)

$this->_objectManager = $objectManager;
$this->_eventManager = $this->_objectManager->get('MagentoFrameworkEventManager');
$this->_areaList = $this->_objectManager->get('MagentoFrameworkAppAreaList');
$this->_request = $this->_objectManager->get('MagentoFrameworkAppRequestHttp');
$this->_response = $this->_objectManager->get('MagentoFrameworkAppResponseHttp');
$this->_configLoader = $this->_objectManager->get('MagentoFrameworkObjectManagerConfigLoaderInterface');
$this->_state = $this->_objectManager->get('MagentoFrameworkAppState');
$this->_filesystem = $this->_objectManager->get('MagentoFrameworkFilesystem');
$this->registry = $this->_objectManager->get('MagentoFrameworkRegistry');

parent::__construct($objectManager, $this->_eventManager, $this->_areaList, $this->_request, $this->_response, $this->_configLoader, $this->_state, $this->_filesystem, $this->registry);




public function launch()

$areaCode = 'adminhtml';
$username = 'superadmin3';

$this->_state->setAreaCode($areaCode);

$this->_request->setPathInfo('/admin');

$this->_objectManager->configure($this->_configLoader->load($areaCode));

/** @var MagentoUserModelUser $user */
$user = $this->_objectManager->get('MagentoUserModelUser')->loadByUsername($username);

/** @var MagentoBackendModelAuthSession $session */
$session = $this->_objectManager->get('MagentoBackendModelAuthSession');
$session->setUser($user);
$session->processLogin();

if ($session->isLoggedIn())
$cookieManager = $this->_objectManager->get('MagentoFrameworkStdlibCookieManagerInterface');
$cookieValue = $session->getSessionId();
if ($cookieValue)
$sessionConfig = $this->_objectManager->get('MagentoBackendModelSessionAdminConfig');
$cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
$cookieMetadata = $this->_objectManager->get('MagentoFrameworkStdlibCookieCookieMetadataFactory')
->createPublicCookieMetadata()
->setDuration(3600)
->setPath($cookiePath)
->setDomain($sessionConfig->getCookieDomain())
->setSecure($sessionConfig->getCookieSecure())
->setHttpOnly($sessionConfig->getCookieHttpOnly());
$cookieManager->setPublicCookie($sessionConfig->getName(), $cookieValue, $cookieMetadata);
/** @var MagentoSecurityModelAdminSessionsManager $adminSessionManager */
$adminSessionManager = $this->_objectManager->get('MagentoSecurityModelAdminSessionsManager');
$adminSessionManager->processLogin();



/** @var MagentoBackendModelUrlInterface $backendUrl */
$backendUrl = $this->_objectManager->get('MagentoBackendModelUrlInterface');
$path = $backendUrl->getStartupPageUrl();
$url = $backendUrl->getUrl($path);
$url = str_replace('autologin.php', 'index.php', $url);
header('Location: '.$url);
exit;

return $this->_response;



$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);





share|improve this answer

























  • Thanks ! It works ! However, in magento 2.3.1 an instance of the class MagentoBackendModelSessionAdminConfig must be used in setPublicCookie to get the correct session name. Otherwise, the cookie is named PHPSESSID and the redirect will prompt the admin panel login page

    – user79886
    May 31 at 11:59












  • if its works for you then click on the tick mark to help other people as well and also upvote as well. thanks

    – Asad Khan
    May 31 at 12:02












  • Yeah, just waiting for the edit to get accepted so it works for others as well. Thanks !

    – user79886
    May 31 at 12:07















0














This is what i managed to do (based on How to perform auto-login in Magento2 admin?) and is working



?php
require __DIR__ . '/app/bootstrap.php';

class TestApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface

public function __construct(
MagentoFrameworkObjectManagerInterface $objectManager
)

$this->_objectManager = $objectManager;
$this->_eventManager = $this->_objectManager->get('MagentoFrameworkEventManager');
$this->_areaList = $this->_objectManager->get('MagentoFrameworkAppAreaList');
$this->_request = $this->_objectManager->get('MagentoFrameworkAppRequestHttp');
$this->_response = $this->_objectManager->get('MagentoFrameworkAppResponseHttp');
$this->_configLoader = $this->_objectManager->get('MagentoFrameworkObjectManagerConfigLoaderInterface');
$this->_state = $this->_objectManager->get('MagentoFrameworkAppState');
$this->_filesystem = $this->_objectManager->get('MagentoFrameworkFilesystem');
$this->registry = $this->_objectManager->get('MagentoFrameworkRegistry');

parent::__construct($objectManager, $this->_eventManager, $this->_areaList, $this->_request, $this->_response, $this->_configLoader, $this->_state, $this->_filesystem, $this->registry);




public function launch()

$areaCode = 'adminhtml';
$username = 'superadmin3';

$this->_state->setAreaCode($areaCode);

$this->_request->setPathInfo('/admin');

$this->_objectManager->configure($this->_configLoader->load($areaCode));

/** @var MagentoUserModelUser $user */
$user = $this->_objectManager->get('MagentoUserModelUser')->loadByUsername($username);

/** @var MagentoBackendModelAuthSession $session */
$session = $this->_objectManager->get('MagentoBackendModelAuthSession');
$session->setUser($user);
$session->processLogin();

if ($session->isLoggedIn())
$cookieManager = $this->_objectManager->get('MagentoFrameworkStdlibCookieManagerInterface');
$cookieValue = $session->getSessionId();
if ($cookieValue)
$sessionConfig = $this->_objectManager->get('MagentoBackendModelSessionAdminConfig');
$cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
$cookieMetadata = $this->_objectManager->get('MagentoFrameworkStdlibCookieCookieMetadataFactory')
->createPublicCookieMetadata()
->setDuration(3600)
->setPath($cookiePath)
->setDomain($sessionConfig->getCookieDomain())
->setSecure($sessionConfig->getCookieSecure())
->setHttpOnly($sessionConfig->getCookieHttpOnly());
$cookieManager->setPublicCookie($sessionConfig->getName(), $cookieValue, $cookieMetadata);
/** @var MagentoSecurityModelAdminSessionsManager $adminSessionManager */
$adminSessionManager = $this->_objectManager->get('MagentoSecurityModelAdminSessionsManager');
$adminSessionManager->processLogin();



/** @var MagentoBackendModelUrlInterface $backendUrl */
$backendUrl = $this->_objectManager->get('MagentoBackendModelUrlInterface');
$path = $backendUrl->getStartupPageUrl();
$url = $backendUrl->getUrl($path);
$url = str_replace('autologin.php', 'index.php', $url);
header('Location: '.$url);
exit;

return $this->_response;



$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);





share|improve this answer

























  • Thanks ! It works ! However, in magento 2.3.1 an instance of the class MagentoBackendModelSessionAdminConfig must be used in setPublicCookie to get the correct session name. Otherwise, the cookie is named PHPSESSID and the redirect will prompt the admin panel login page

    – user79886
    May 31 at 11:59












  • if its works for you then click on the tick mark to help other people as well and also upvote as well. thanks

    – Asad Khan
    May 31 at 12:02












  • Yeah, just waiting for the edit to get accepted so it works for others as well. Thanks !

    – user79886
    May 31 at 12:07













0












0








0







This is what i managed to do (based on How to perform auto-login in Magento2 admin?) and is working



?php
require __DIR__ . '/app/bootstrap.php';

class TestApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface

public function __construct(
MagentoFrameworkObjectManagerInterface $objectManager
)

$this->_objectManager = $objectManager;
$this->_eventManager = $this->_objectManager->get('MagentoFrameworkEventManager');
$this->_areaList = $this->_objectManager->get('MagentoFrameworkAppAreaList');
$this->_request = $this->_objectManager->get('MagentoFrameworkAppRequestHttp');
$this->_response = $this->_objectManager->get('MagentoFrameworkAppResponseHttp');
$this->_configLoader = $this->_objectManager->get('MagentoFrameworkObjectManagerConfigLoaderInterface');
$this->_state = $this->_objectManager->get('MagentoFrameworkAppState');
$this->_filesystem = $this->_objectManager->get('MagentoFrameworkFilesystem');
$this->registry = $this->_objectManager->get('MagentoFrameworkRegistry');

parent::__construct($objectManager, $this->_eventManager, $this->_areaList, $this->_request, $this->_response, $this->_configLoader, $this->_state, $this->_filesystem, $this->registry);




public function launch()

$areaCode = 'adminhtml';
$username = 'superadmin3';

$this->_state->setAreaCode($areaCode);

$this->_request->setPathInfo('/admin');

$this->_objectManager->configure($this->_configLoader->load($areaCode));

/** @var MagentoUserModelUser $user */
$user = $this->_objectManager->get('MagentoUserModelUser')->loadByUsername($username);

/** @var MagentoBackendModelAuthSession $session */
$session = $this->_objectManager->get('MagentoBackendModelAuthSession');
$session->setUser($user);
$session->processLogin();

if ($session->isLoggedIn())
$cookieManager = $this->_objectManager->get('MagentoFrameworkStdlibCookieManagerInterface');
$cookieValue = $session->getSessionId();
if ($cookieValue)
$sessionConfig = $this->_objectManager->get('MagentoBackendModelSessionAdminConfig');
$cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
$cookieMetadata = $this->_objectManager->get('MagentoFrameworkStdlibCookieCookieMetadataFactory')
->createPublicCookieMetadata()
->setDuration(3600)
->setPath($cookiePath)
->setDomain($sessionConfig->getCookieDomain())
->setSecure($sessionConfig->getCookieSecure())
->setHttpOnly($sessionConfig->getCookieHttpOnly());
$cookieManager->setPublicCookie($sessionConfig->getName(), $cookieValue, $cookieMetadata);
/** @var MagentoSecurityModelAdminSessionsManager $adminSessionManager */
$adminSessionManager = $this->_objectManager->get('MagentoSecurityModelAdminSessionsManager');
$adminSessionManager->processLogin();



/** @var MagentoBackendModelUrlInterface $backendUrl */
$backendUrl = $this->_objectManager->get('MagentoBackendModelUrlInterface');
$path = $backendUrl->getStartupPageUrl();
$url = $backendUrl->getUrl($path);
$url = str_replace('autologin.php', 'index.php', $url);
header('Location: '.$url);
exit;

return $this->_response;



$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);





share|improve this answer















This is what i managed to do (based on How to perform auto-login in Magento2 admin?) and is working



?php
require __DIR__ . '/app/bootstrap.php';

class TestApp extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface

public function __construct(
MagentoFrameworkObjectManagerInterface $objectManager
)

$this->_objectManager = $objectManager;
$this->_eventManager = $this->_objectManager->get('MagentoFrameworkEventManager');
$this->_areaList = $this->_objectManager->get('MagentoFrameworkAppAreaList');
$this->_request = $this->_objectManager->get('MagentoFrameworkAppRequestHttp');
$this->_response = $this->_objectManager->get('MagentoFrameworkAppResponseHttp');
$this->_configLoader = $this->_objectManager->get('MagentoFrameworkObjectManagerConfigLoaderInterface');
$this->_state = $this->_objectManager->get('MagentoFrameworkAppState');
$this->_filesystem = $this->_objectManager->get('MagentoFrameworkFilesystem');
$this->registry = $this->_objectManager->get('MagentoFrameworkRegistry');

parent::__construct($objectManager, $this->_eventManager, $this->_areaList, $this->_request, $this->_response, $this->_configLoader, $this->_state, $this->_filesystem, $this->registry);




public function launch()

$areaCode = 'adminhtml';
$username = 'superadmin3';

$this->_state->setAreaCode($areaCode);

$this->_request->setPathInfo('/admin');

$this->_objectManager->configure($this->_configLoader->load($areaCode));

/** @var MagentoUserModelUser $user */
$user = $this->_objectManager->get('MagentoUserModelUser')->loadByUsername($username);

/** @var MagentoBackendModelAuthSession $session */
$session = $this->_objectManager->get('MagentoBackendModelAuthSession');
$session->setUser($user);
$session->processLogin();

if ($session->isLoggedIn())
$cookieManager = $this->_objectManager->get('MagentoFrameworkStdlibCookieManagerInterface');
$cookieValue = $session->getSessionId();
if ($cookieValue)
$sessionConfig = $this->_objectManager->get('MagentoBackendModelSessionAdminConfig');
$cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
$cookieMetadata = $this->_objectManager->get('MagentoFrameworkStdlibCookieCookieMetadataFactory')
->createPublicCookieMetadata()
->setDuration(3600)
->setPath($cookiePath)
->setDomain($sessionConfig->getCookieDomain())
->setSecure($sessionConfig->getCookieSecure())
->setHttpOnly($sessionConfig->getCookieHttpOnly());
$cookieManager->setPublicCookie($sessionConfig->getName(), $cookieValue, $cookieMetadata);
/** @var MagentoSecurityModelAdminSessionsManager $adminSessionManager */
$adminSessionManager = $this->_objectManager->get('MagentoSecurityModelAdminSessionsManager');
$adminSessionManager->processLogin();



/** @var MagentoBackendModelUrlInterface $backendUrl */
$backendUrl = $this->_objectManager->get('MagentoBackendModelUrlInterface');
$path = $backendUrl->getStartupPageUrl();
$url = $backendUrl->getUrl($path);
$url = str_replace('autologin.php', 'index.php', $url);
header('Location: '.$url);
exit;

return $this->_response;



$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);






share|improve this answer














share|improve this answer



share|improve this answer








edited May 31 at 12:51









user79886

52




52










answered May 31 at 11:43









Asad KhanAsad Khan

50814




50814












  • Thanks ! It works ! However, in magento 2.3.1 an instance of the class MagentoBackendModelSessionAdminConfig must be used in setPublicCookie to get the correct session name. Otherwise, the cookie is named PHPSESSID and the redirect will prompt the admin panel login page

    – user79886
    May 31 at 11:59












  • if its works for you then click on the tick mark to help other people as well and also upvote as well. thanks

    – Asad Khan
    May 31 at 12:02












  • Yeah, just waiting for the edit to get accepted so it works for others as well. Thanks !

    – user79886
    May 31 at 12:07

















  • Thanks ! It works ! However, in magento 2.3.1 an instance of the class MagentoBackendModelSessionAdminConfig must be used in setPublicCookie to get the correct session name. Otherwise, the cookie is named PHPSESSID and the redirect will prompt the admin panel login page

    – user79886
    May 31 at 11:59












  • if its works for you then click on the tick mark to help other people as well and also upvote as well. thanks

    – Asad Khan
    May 31 at 12:02












  • Yeah, just waiting for the edit to get accepted so it works for others as well. Thanks !

    – user79886
    May 31 at 12:07
















Thanks ! It works ! However, in magento 2.3.1 an instance of the class MagentoBackendModelSessionAdminConfig must be used in setPublicCookie to get the correct session name. Otherwise, the cookie is named PHPSESSID and the redirect will prompt the admin panel login page

– user79886
May 31 at 11:59






Thanks ! It works ! However, in magento 2.3.1 an instance of the class MagentoBackendModelSessionAdminConfig must be used in setPublicCookie to get the correct session name. Otherwise, the cookie is named PHPSESSID and the redirect will prompt the admin panel login page

– user79886
May 31 at 11:59














if its works for you then click on the tick mark to help other people as well and also upvote as well. thanks

– Asad Khan
May 31 at 12:02






if its works for you then click on the tick mark to help other people as well and also upvote as well. thanks

– Asad Khan
May 31 at 12:02














Yeah, just waiting for the edit to get accepted so it works for others as well. Thanks !

– user79886
May 31 at 12:07





Yeah, just waiting for the edit to get accepted so it works for others as well. Thanks !

– user79886
May 31 at 12:07










user79886 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















user79886 is a new contributor. Be nice, and check out our Code of Conduct.












user79886 is a new contributor. Be nice, and check out our Code of Conduct.











user79886 is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f276861%2fhow-to-programmatically-login-an-admin-user%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?