trstill Ответов: 1

Создайте функцию автоматического входа в систему в MVC 5 identity


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.


Что я уже пробовал:

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.

1 Ответов

Рейтинг:
0

Christian Graus

Вы можете просто написать свой собственный код для входа в систему, вы действительно можете сделать это без пароля, по памяти. Добавьте свой собственный метод к контроллеру. Это, конечно, очень плохая идея