How to make multi-step login to IdentityServer4?

We used IdentityServer3, implicit provisioning and login consisted of several screens. IdentityServer3 has built-in support for such a multi-user login workflow (for example, for accepting EULA, two-factor login, etc.). A function called "partial login" is even an example: https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source/CustomUserService/CustomUserService

We recently upgraded to AspNetCore and IdentityServer4 and wondered how to assume that the same could be achieved. That is, check the username and password in the first step, and if correct, save it securely (for example, in an encrypted cookie) for the next step.

+4
source share
1 answer

IdentityServer3 partial login replication was our solution: use a custom cookie to store data between steps.

First, we need to register our user cookie authentication (in Startup.Configure)

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationScheme = "my-partial",
    AutomaticAuthenticate = false,
    AutomaticChallenge = false
});
  • The first entry / entry point into the entry workflow must be mapped to GET /account/login(as from IdentityServer4 1.0.0-rc2).

  • In the second step, after sending and verifying the credentials, we save the username (and, ultimately, any other data) in a cookie.

the code:

var claims = new []
{
    new Claim("my-user", username),
    new Claim("some-attribute", someAttribute)
};

await HttpContext.Authentication
    .SignInAsync("my-partial", new ClaimsPrincipal(new ClaimsIdentity(claims)));

. POST /account/login . , IdentityServer ( RC2). .

    • cookie
    • cookie
    • ""
    • returnUrl ( . )

var partialUser = await HttpContext.Authentication.AuthenticateAsync("my-partial");
var username = partialUser?.Claims.FirstOrDefault(c => c.Type == "dr-user")?.Value;

var claims = new [] { /* Your custom claims */};

await HttpContext.Authentication
    .SignOutAsync("my-partial");

await HttpContext.Authentication
    .SignInAsync(username, username, claims);

return Redirect(returnUrl);

, , , , cookie ..

+7

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


All Articles