I am using Azure AAD B2C to manage my users and auth flow. I am trying to create a user experience where the user can log in under a different account. The user should be able to Log Out of their account and then click Sign In and be able to provide a different username and password.
However, currently I sign out. I can confirm through F12 debugger that all cookies are cleared. Then I click sign in and it signs me back into my previous account without challenging me for a username and password.
I'm not sure what is happening or why. Here is my Sign In code.
public IActionResult SignIn([FromRoute] string scheme)
{
scheme ??= OpenIdConnectDefaults.AuthenticationScheme;
var redirectUrl = Url.Content("~/");
var properties = new AuthenticationProperties { RedirectUri = redirectUrl, AllowRefresh = true };
properties.Items["policy"] = "B2C_1_SignUpIn";
return Challenge(properties, scheme);
}
Here is my Sign Out code.
public async Task<IActionResult> SignOutAsync([FromRoute] string scheme)
{
HttpContext.Session.Clear();
if (HttpContext.Request.Cookies.Count > 0)
{
var test = HttpContext.Request.Cookies.ToList();
var siteCookies = HttpContext.Request.Cookies.Where(c => c.Key.Contains(".AspNetCore.") || c.Key.Contains("Microsoft.Authentication"));
foreach (var cookie in siteCookies)
{
Response.Cookies.Delete(cookie.Key);
}
}
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
return Redirect("~/");
}
My Startup.cs looks like this.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddMicrosoftIdentityWebAppAuthentication(
Configuration,
"AzureAdB2C",
OpenIdConnectDefaults.AuthenticationScheme,
CookieAuthenticationDefaults.AuthenticationScheme);
services.AddMvc();
services.AddSession();
services.AddRazorPages();
services.AddControllersWithViews();
services.Configure<OpenIdConnectOptions>(Configuration.GetSection("AzureAdB2C"));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication().UseCookiePolicy();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
//endpoints.MapAreaControllerRoute(
// name: "Identity",
// areaName: "Identity",
// pattern: "Identity/{controller=Home}/{action=Index}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}");
endpoints.MapRazorPages();
});
}
}
My UI references the User.Identity.IsAuthenicated to determine if user information is present. Any help why it is behaving this way by automatically signing me in after I have signed out would be very helpful. I am new to OIDC and Azure B2C but I feel like I'm missing something very basic. Thanks in advance.
