Skip to content

Commit 2070747

Browse files
committed
feat: support identifiers and first_screen sign-in params
1 parent f30361c commit 2070747

File tree

5 files changed

+108
-8
lines changed

5 files changed

+108
-8
lines changed

samples/sample-blazor/Program.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,21 @@
4747
{
4848
if (!(context.User?.Identity?.IsAuthenticated ?? false))
4949
{
50-
await context.ChallengeAsync(new AuthenticationProperties { RedirectUri = "/" });
51-
} else {
50+
var authProperties = new AuthenticationProperties
51+
{
52+
RedirectUri = "/"
53+
};
54+
55+
authProperties.SetParameter("first_screen", LogtoParameters.Authentication.FirstScreen.Register);
56+
authProperties.SetParameter("identifiers", string.Join(",", new[]
57+
{
58+
LogtoParameters.Authentication.Identifiers.Username,
59+
}));
60+
61+
await context.ChallengeAsync(authProperties);
62+
}
63+
else
64+
{
5265
context.Response.Redirect("/");
5366
}
5467
});

samples/sample-mvc/Controllers/HomeController.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,18 @@ public async Task<IActionResult> Index()
2525

2626
public IActionResult SignIn()
2727
{
28-
return Challenge(new AuthenticationProperties { RedirectUri = "/" });
28+
var authProperties = new AuthenticationProperties
29+
{
30+
RedirectUri = "/"
31+
};
32+
33+
authProperties.SetParameter("first_screen", LogtoParameters.Authentication.FirstScreen.SignIn);
34+
authProperties.SetParameter("identifiers", string.Join(",", new[]
35+
{
36+
LogtoParameters.Authentication.Identifiers.Username,
37+
}));
38+
39+
return Challenge(authProperties);
2940
}
3041

3142
// Use the `new` keyword to avoid conflict with the `ControllerBase.SignOut` method

samples/sample/Pages/Index.cshtml.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ public async Task OnGetAsync()
2222

2323
public async Task OnPostSignInAsync()
2424
{
25-
await HttpContext.ChallengeAsync(new AuthenticationProperties { RedirectUri = "/" });
25+
var authProperties = new AuthenticationProperties
26+
{
27+
RedirectUri = "/"
28+
};
29+
30+
authProperties.SetParameter("first_screen", LogtoParameters.Authentication.FirstScreen.Register);
31+
authProperties.SetParameter("identifiers", string.Join(",", new[]
32+
{
33+
LogtoParameters.Authentication.Identifiers.Username,
34+
}));
35+
36+
await HttpContext.ChallengeAsync(authProperties);
2637
}
2738

2839
public async Task OnPostSignOutAsync()

src/Logto.AspNetCore.Authentication/LogtoParameters.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,57 @@ public static class Claims
115115
/// </summary>
116116
public const string Identities = "identities";
117117
}
118+
119+
/// <summary>
120+
/// The authentication parameters for Logto sign-in experience customization.
121+
/// </summary>
122+
public static class Authentication
123+
{
124+
/// <summary>
125+
/// The first screen to show in the sign-in experience.
126+
/// </summary>
127+
public static class FirstScreen
128+
{
129+
/// <summary>
130+
/// Show the register form first.
131+
/// </summary>
132+
public const string Register = "identifier:register";
133+
134+
/// <summary>
135+
/// Show the sign-in form first.
136+
/// </summary>
137+
public const string SignIn = "identifier:sign_in";
138+
139+
/// <summary>
140+
/// Show the single sign-on form first.
141+
/// </summary>
142+
public const string SingleSignOn = "single_sign_on";
143+
144+
/// <summary>
145+
/// Show the reset password form first.
146+
/// </summary>
147+
public const string ResetPassword = "reset_password";
148+
}
149+
150+
/// <summary>
151+
/// The identifiers to use for authentication.
152+
/// </summary>
153+
public static class Identifiers
154+
{
155+
/// <summary>
156+
/// Use email for authentication.
157+
/// </summary>
158+
public const string Email = "email";
159+
160+
/// <summary>
161+
/// Use phone for authentication.
162+
/// </summary>
163+
public const string Phone = "phone";
164+
165+
/// <summary>
166+
/// Use username for authentication.
167+
/// </summary>
168+
public const string Username = "username";
169+
}
170+
}
118171
}

src/Logto.AspNetCore.Authentication/extensions/AuthenticationBuilderExtensions.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Logto.AspNetCore.Authentication;
99
using Microsoft.IdentityModel.Tokens;
1010
using System;
1111
using System.Collections.Generic;
12+
using System.Threading.Tasks;
1213

1314
/// <summary>
1415
/// Extension methods to configure Logto authentication.
@@ -101,15 +102,26 @@ private static void ConfigureOpenIdConnectOptions(OpenIdConnectOptions options,
101102
options.ClaimActions.MapAllExcept("nbf", "nonce", "c_hash", "at_hash");
102103
options.Events = new OpenIdConnectEvents
103104
{
105+
OnRedirectToIdentityProvider = context =>
106+
{
107+
if (context.Properties.Parameters.TryGetValue("first_screen", out var firstScreen))
108+
{
109+
context.ProtocolMessage.Parameters.Add("first_screen", firstScreen?.ToString());
110+
}
111+
112+
if (context.Properties.Parameters.TryGetValue("identifiers", out var identifiers))
113+
{
114+
context.ProtocolMessage.Parameters.Add("identifiers", identifiers?.ToString());
115+
}
116+
117+
return Task.CompletedTask;
118+
},
104119
OnRedirectToIdentityProviderForSignOut = async context =>
105120
{
106-
// Clean up the cookie when signing out.
107121
await context.HttpContext.SignOutAsync(cookieScheme);
108-
109-
// Rebuild parameters since we use <c>client_id</c> for sign-out, no need to use <c>id_token_hint</c>.
110122
context.ProtocolMessage.Parameters.Remove(OpenIdConnectParameterNames.IdTokenHint);
111123
context.ProtocolMessage.Parameters.Add(OpenIdConnectParameterNames.ClientId, logtoOptions.AppId);
112-
},
124+
}
113125
};
114126
options.TokenValidationParameters = new TokenValidationParameters
115127
{

0 commit comments

Comments
 (0)