Asp.Net Core - Simple Form Authentication

I have this old MVC5 application that uses forms authentication in the simplest form possible. Only one account is stored in web.config, no roles, etc.

<authentication mode="Forms">
  <forms loginUrl="~/Login/Index" timeout="30">
    <credentials passwordFormat="Clear">
      <user name="some-user" password="some-password" />
    </credentials>
  </forms>
</authentication>

The login procedure simply calls

FormsAuthentication.Authenticate(name, password);

What is it. Is there something similar (in terms of simplicity) in the asp.net core?

+6
source share
2 answers

It is not so easy:)

  • In Startup.cs, configure the method.

    app.UseCookieAuthentication(options =>
    {
      options.AutomaticAuthenticate = true;
      options.AutomaticChallenge = true;
      options.LoginPath = "/Home/Login";
    });
    
  • Add an Authorize attribute to protect the resources you want to protect.

    [Authorize]
    public IActionResult Index()
    {
      return View();
    }
    
  • In the Home Controller login method, write the following method.

    var username = Configuration["username"];
    var password = Configuration["password"];
    if (authUser.Username == username && authUser.Password == password)
    {
      var identity = new ClaimsIdentity(claims, 
          CookieAuthenticationDefaults.AuthenticationScheme);
    
      HttpContext.Authentication.SignInAsync(
        CookieAuthenticationDefaults.AuthenticationScheme,
        new ClaimsPrincipal(identity));
    
      return Redirect("~/Home/Index");
    }
    else
    {
      ModelState.AddModelError("","Login failed. Please check Username and/or password");
    }
    

github : https://github.com/anuraj/CookieAuthMVCSample

+10

, "" .Net... , "" System.Web.Security. , . - .

0

Source: https://habr.com/ru/post/1017307/


All Articles