79129777

Date: 2024-10-27 01:46:43
Score: 1
Natty:
Report link

Based on this article, one can answer this question as follows:

Program.cs

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect("appOne", options =>
{
    builder.Configuration.GetSection("AppOne").Bind(options);
    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.ResponseType = OpenIdConnectResponseType.Code;
    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        NameClaimType = "name"
    };
    options.MapInboundClaims = false;
})
.AddOpenIdConnect("appTwo", options => 
{
    builder.Configuration.GetSection("AppTwo").Bind(options);
    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.ResponseType = OpenIdConnectResponseType.Code;
    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        NameClaimType = "name"
    };
});

appsettings.json

"AppOne": {
    "Authority": "***",
    "ClientId": "***",
    "ClientSecret": "***",
    "CallbackPath": "***",
    "SignedOutCallbackPath": "***"
},
"AppTwo": {
    "Authority": "***",
    "ClientId": "***",
    "ClientSecret": "***",
    "CallbackPath": "***",
    "SignedOutCallbackPath": "***"
}

Controller:

[AllowAnonymous]
[HttpGet]
public ActionResult LoginAppOne(string returnUrl)
{
    return Challenge(new AuthenticationProperties
    {
        RedirectUri = !string.IsNullOrEmpty(returnUrl) ? returnUrl : "/", 
    }, "appOne");
}
 
[AllowAnonymous]
[HttpGet]
public ActionResult LoginAppTwo(string returnUrl)
{
    return Challenge(new AuthenticationProperties
    {
        RedirectUri = !string.IsNullOrEmpty(returnUrl) ? returnUrl : "/"
    }, "appTwo");
}

View:

<li class="nav-item">
    <a class="nav-link text-dark"
       href="~/api/Account/LoginAppOne">Login App One</a>
</li>
<li class="nav-item">
    <a class="nav-link text-dark"
       href="~/api/Account/LoginAppTwo">LOgin App Two</a>
</li>
Reasons:
  • Blacklisted phrase (1): this article
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: StanSm789