Click here to Skip to main content
14,826,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Create Auto Login function in MVC 5 Identity
New to MVC / Identity
I am trying to create a function that will automatically login a user based on some hard coded credentials.
This code is called when a page is loaded and is triggered by a app setting.
That part is working ok.
The code so far (in the AccountController):
public ActionResult AutoLogin()
 
{
    var account = new AccountController();
    TCDec2019.Models.LoginViewModel mdl = new LoginViewModel();
    mdl.Email = "trstillg@gmail.com";
    mdl.Password = "Abc#1def";
    mdl.RememberMe = false;
   var result = Login(mdl, null);  //executes the standard login function
    return null;
}

The code works until I call the standard Login function in the Accounts Controller using the mdl model created with the code.
The line in the Login task giving the problem is:
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
the result is set by calling ApplicationSignInManager in the AccountController
This call returns an Exception System.NulReference.

If I use the standard login function, which calls the same ApplicationSignManager it returns 
{TCDec2019.ApplicationSignInManager}

So, my question is what do I need to set so that calling the ApplicationSigninManager will return the correct value.

Thanks in advance.


What I have tried:

Create Auto Login function in MVC 5 Identity
New to MVC / Identity
I am trying to create a function that will automatically login a user based on some hard coded credentials.
This code is called when a page is loaded and is triggered by a app setting.
That part is working ok.
The code so far (in the AccountController):
public ActionResult AutoLogin()
 
{
    var account = new AccountController();
    TCDec2019.Models.LoginViewModel mdl = new LoginViewModel();
    mdl.Email = "trstillg@gmail.com";
    mdl.Password = "Abc#1def";
    mdl.RememberMe = false;
   var result = Login(mdl, null);  //executes the standard login function
    return null;
}

The code works until I call the standard Login function in the Accounts Controller using the mdl model created with the code.
The line in the Login task giving the problem is:
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
the result is set by calling ApplicationSignInManager in the AccountController
This call returns an Exception System.NulReference.

If I use the standard login function, which calls the same ApplicationSignManager it returns 
{TCDec2019.ApplicationSignInManager}

So, my question is what do I need to set so that calling the ApplicationSigninManager will return the correct value.

Thanks in advance.
Posted
Updated 5-Jan-20 16:54pm

1 solution

You can just write your own code to login, you actually can do it without a password, from memory. Add your own method to the controller. This is, of course, a spectacularly bad idea
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900