2

ISSUE

I would like to be able to set the properties (IsPersistent & ExpiresUtc) on the default challenge request for AzureAD login. I am not seeing how to do this, so that the user cookie that is created last longer than the browser session (closing then reopening browser).

Right now the only way I can set these properties for the challenge is to have the user manually log in by sending them to the following action.

[HttpGet]
public IActionResult SignIn()
{
    return Challenge(
        new AuthenticationProperties { RedirectUri = "/", IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddDays(10) },
            OpenIdConnectDefaults.AuthenticationScheme);
    }

I would rather force the user to auth as soon as they enter the site by using the following code registered in startup.cs

services.AddMvc(options =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});

The base template I used to start my project was from the following command

dotnet new mvc --auth SingleOrg --client-id <Application (client) ID> --tenant-id <Directory (tenant) ID>

When I then run the site, it will challenge the first incoming request and start me down the path of logging in and returning me to the application. This works just fine with one exception. The cookie that is sent back to the client has an expiration of December 31 1969.

I then sign out of the application using the default SignOut link without issue.

To log in I could use the default login link

<a class="nav-link text-dark" 
    asp-area="AzureAD" 
    asp-controller="Account" 
    asp-action="SignIn">Sign in</a>

or my custom link pointing to the home controller with the action code from above.

<a class="nav-link text-dark" 
    asp-area="" 
    asp-controller="Home" 
    asp-action="SignIn">Sign in</a>

If I use the default link I go through the authentication process again and again the cookie again has the default date, but if I click on my custom link the cookie comes back with an expiration 10 days in the future and lasts across browser sessions. I want the default behavior of the challenge to be the same as the challenge I wrote.

I have read various posts to no avail and need some help please.

Craig Selbert
  • 737
  • 1
  • 8
  • 17

0 Answers0