Reputation: 396
I am developing a Blazor Web Assembly app in Visual Studio that uses Azure AD B2C for authentication. It is all setup and working well except that developing is a chore because each time I want to launch the debugger and open the browser to review my work I am required to login in again to view pages that require authentication. This is a time consuming and annoying step that I would hope to avoid.
Ideally there would be a way to stay logged in between debug browser sessions launched from Visual Studio.
I have searched for the answer on the web with not luck yet. Anyone have any ideas on how to do this?
Upvotes: 4
Views: 1443
Reputation: 879
In order to solve this problem with Blazor WASM Hosted with IdentityServer4 or Duende latest Identity do the following
Create Controller in Server side project
public DevAuthController(UserManager<Korisnik> userManager, SignInManager<Korisnik> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
[HttpGet]
public async Task<IActionResult> SignInDevUser()
{
var user = await _userManager.FindByNameAsync("username");
if (user != null)
{
await _signInManager.SignInAsync(user, isPersistent: true);
// Redirect to the Index page or another route of your choice
return Redirect("/");
}
return NotFound("Dev user not found");
}
go to the server project -> properties -> debug -> open debug launch profiles UI -> enter the value in URL e.g. https://localhost:7060/api/devsignin
Upvotes: 0
Reputation: 1
When it happens that you do not stay logged in when you debug a web application with Visual Studio, first look at the icon of your profile (in Edge upper left corner). If it is an empty icon, sync it to your account and from that point on you should be staying logged in again. It also happened to me that for some reason the connection to my account was lost for a particular web application (the problem was really connected to the name of the application). Syncing my account in the browser window for debugging, solved the problem. It remains unclear, though, why the connection to my account was lost.
Upvotes: 0
Reputation: 683
In Program.cs, set MsalCacheOptions.CacheLocation to "localStorage"
Example:
builder.Services.AddMsalAuthentication(options =>
{
// Keeps logged in between debugging sessions
options.ProviderOptions.Cache.CacheLocation = "localStorage";
// Set up other options
...
});
Upvotes: 6
Reputation: 396
One work around I have found is to open a second browser window to the local debugging endpoint (https://localhost:5001/ in my case). When I login to the app in this browser and then refresh the browser I will stay logged in.
So, as I work, when I am ready to review something, I launch the debugger in Visual Studio which opens a new browser window, but then I ignore that window and refresh the first window I already had open in which I was already logged in and review my work there. It is a couple extra clicks but I don't need to login again.
Upvotes: 0