If you are using .NET with OpenID Connection (OIDC), we suggest the following.
Have a program.cs/startup.cs file, something like this:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = Configuration["Kinde:Domain"];
options.ClientId = Configuration["Kinde:ClientId"];
options.ClientSecret = Configuration["Kinde:ClientSecret"];
options.ResponseType = "code";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
// Set other necessary options
});
Have a middleware for pipeline program.cs or configure method of startup.cs:
app.UseAuthentication();
app.UseAuthorization();
Then the authentication should be handled automatically:
[HttpGet]
public async Task<IActionResult> Callback()
{
// This should be handled automatically by the OpenID Connect middleware
// You can perform additional actions here if necessary
return LocalRedirect(Url.Content("~/")); // Redirect to home or other page
}
Then you can check it with:
@inject AuthenticationStateProvider AuthenticationStateProvider
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
// User is authenticated
}
}
Here are other tips we recommend:
Microsoft.AspNetCore.Authentication.OpenIdConnect
from NuGet.https://localhost:7040/signin-oidc
(port might be different) as a callback URL in your Kinde backend application