Skip to content

Commit

Permalink
Remember selected user in Localtest cookie (#9683)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne authored Jan 31, 2023
1 parent d80590b commit 487b83d
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public async Task<IActionResult> Index()
model.App = model.TestApps[0].Value?.Split("/").LastOrDefault();
}
model.TestUsers = await GetTestUsersForList();
model.UserSelect = Request.Cookies["Localtest_User.Party_Select"];
var defaultAuthLevel = await GetAppAuthLevel(model.AppModeIsHttp, model.TestApps);
model.AuthenticationLevels = GetAuthenticationLevels(defaultAuthLevel);
}
Expand Down Expand Up @@ -149,7 +150,7 @@ public async Task<ActionResult> LogInTestUser(StartAppModel startAppModel)
int authenticationLevel = Convert.ToInt32(startAppModel.AuthenticationLevel);

string token = await _authenticationService.GenerateTokenForProfile(profile, authenticationLevel);
CreateJwtCookieAndAppendToResponse(token, startAppModel.PartyId);
CreateJwtCookieAndAppendToResponse(token, startAppModel.PartyId, startAppModel.UserSelect);
}

if (startAppModel.AppPathSelection?.Equals("accessmanagement") == true)
Expand Down Expand Up @@ -424,7 +425,7 @@ private static SelectListItem GetSelectItem(Application app, string path)
/// Creates a session cookie meant to be used to hold the generated JSON Web Token and appends it to the response.
/// </summary>
/// <param name="cookieValue">The cookie value.</param>
private void CreateJwtCookieAndAppendToResponse(string identityCookie, int altinnPartyId)
private void CreateJwtCookieAndAppendToResponse(string identityCookie, int altinnPartyId, string userSelect)
{
ICookieManager cookieManager = new ChunkingCookieManager();

Expand Down Expand Up @@ -463,6 +464,24 @@ private void CreateJwtCookieAndAppendToResponse(string identityCookie, int altin
partyCookieBuilder.Name,
altinnPartyId.ToString(),
partyCookieOptions);

// Add cookie about users selection (for preselecting in the dropdown)
CookieBuilder userSelectCookieBuilder = new RequestPathBaseCookieBuilder
{
Name = "Localtest_User.Party_Select",
SameSite = SameSiteMode.Lax,
HttpOnly = false,
SecurePolicy = CookieSecurePolicy.None,
IsEssential = true,
Domain = _generalSettings.Hostname,
Expiration = TimeSpan.MaxValue,
};
CookieOptions userSelectCookieOptions = cookieBuilder.Build(HttpContext);
cookieManager.AppendResponseCookie(
HttpContext,
userSelectCookieBuilder.Name,
userSelect,
userSelectCookieOptions);
}
}
}

0 comments on commit 487b83d

Please sign in to comment.