I have a very basic authentication setup:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie();
    }
}

Now when the MVC ChallengeResult is returned (with a AuthenticationProperties argument):

public class HomeController : Controller
{ 
    public IActionResult Index()
    {
        if (!User.Identity.IsAuthenticated)
        {
            return Challenge(new AuthenticationProperties
            {
                IsPersistent = true
            });
        }
        else
        {
            return View();    
        }
    }
}

The request is redirected to /Account/Login and to the following action method:

enter image description here

Issue: the original assignment of IsPersistent = true in the Index() action method is missing when the execution reach the Login() action method.

The App is built in: .NET Core 3.1

1

There are 1 best solutions below

2
On BEST ANSWER

The argument properties in action login is a new instance. It will be always null. Because you do not assign a value on a request. After returning to Challenge, the authentication will initiate a redirect. But the redirect will not carry this parameter.

You can set an event to save the value of the cookie authentication context in the session.

public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication("auth")
            .AddCookie("auth",config=>
            {
                config.Cookie.Name = "cookie.name";

                config.Events.OnRedirectToLogin = context =>
                {
                    context.HttpContext.Session.SetString("Properties",System.Text.Json.JsonSerializer.Serialize(context.Properties));
                    context.Response.Redirect("/home/login");
                    return Task.CompletedTask;
                };
            });
        services.AddSession();
        services.AddControllersWithViews();
    }

In action login.

public IActionResult login()
    {
        var get=HttpContext.Session.GetString("Properties");
        var deserializer = System.Text.Json.JsonSerializer.Deserialize<AuthenticationProperties>(get);
        return View();
    }

Then, the properties will be passed into login.

enter image description here