returnUrl is returning null while hit the controller

Controller:-
public IActionResult Login([FromQuery] string returnUrl)
{
var redirectUri = returnUrl is null ? Url.Content(“~/”) : “/” + returnUrl;
if (User.Identity.IsAuthenticated)
{
return LocalRedirect(redirectUri);
}
return Challenge();
}

Program.cs:-
builder.Services.AddAuthentication(authOptions =>
{
authOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddOpenIdConnect(oidcOptions =>
{
oidcOptions.ClientId = builder.Configuration[“Okta:ClientID”];
oidcOptions.ClientSecret = builder.Configuration[“Okta:ClientSecret”];
oidcOptions.CallbackPath = “/authorization-code/callback”;
oidcOptions.Authority = builder.Configuration[“Okta:Issuer”];
oidcOptions.ResponseType = “code”;
oidcOptions.SaveTokens = true;
oidcOptions.Scope.Add(“openid”);
oidcOptions.Scope.Add(“profile”);
oidcOptions.TokenValidationParameters.ValidateIssuer = false;
oidcOptions.TokenValidationParameters.NameClaimType = “name”;
}).AddCookie();

i passed the value in appsettings.json also
url, clientid, clientsecretID
could you please help me out from this matter why the returnurl fetching null??

You have a condition in your redirect uri variable, have you added logging to check if this expression is actually returning a value?

Also, are the quotation marks in your code formatted in the way they are from being copy/pasted or are they this way in the code as well? Ex: “/” vs “/” - this can cause issues as well.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.