簡體   English   中英

Azure B2C 注銷,然后登錄不會使用密碼提示挑戰用戶

[英]Azure B2C Sign Out, then Sign In does not challenge the user with a password prompt

我正在使用 Azure AAD B2C 來管理我的用戶和身份驗證流程。 我正在嘗試創建用戶可以在不同帳戶下登錄的用戶體驗。 用戶應該能夠退出他們的帳戶,然后單擊登錄並能夠提供不同的用戶名和密碼。

但是,目前我退出。 我可以通過 F12 調試器確認所有 cookies 都已清除。 然后我點擊登錄,它讓我重新登錄到我以前的帳戶,而無需詢問我的用戶名和密碼。

我不確定發生了什么或為什么。 這是我的登錄代碼。

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);
    }

這是我的退出代碼。

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("~/");
    }

我的 Startup.cs 看起來像這樣。

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();
        });
    }
}

我的 UI 引用User.Identity.IsAuthenicated以確定是否存在用戶信息。 通過在我退出后自動登錄來幫助我為什么會以這種方式運行的任何幫助都會非常有幫助。 我是 OIDC 和 Azure B2C 的新手,但我覺得我缺少一些非常基本的東西。 提前致謝。

Azure側的登錄狀態你也清除了嗎? 由於 Azure B2C 是您的身份提供商,您需要將用戶從您的應用程序和 B2C 中注銷,以強制他們必須重新輸入其憑據。

這可以通過將您的用戶重定向到 OpenId 配置中的注銷 URL 以從 B2C 端清除身份驗證 cookies 以及為您的應用程序清除身份驗證 Z55E7DD3016CE4AC57B9A0F56C 之后來完成。

您甚至可以包含重定向 URL 作為查詢參數,以將用戶重定向回您網站上的特定頁面。

將以下方法添加到 AccountController 和注銷應該清除本地以及遠程登錄。 這也應該處理注銷重定向。 希望這可以幫助。

[HttpGet("{scheme?}")]
    public IActionResult SignOut(
        [FromRoute] string scheme, 
        [FromQuery] string redirectUri)
    {
        scheme ??= OpenIdConnectDefaults.AuthenticationScheme;
        string redirect;
        if (!string.IsNullOrEmpty(redirectUri) && Url.IsLocalUrl(redirectUri))
        {
            redirect = redirectUri;
        }
        else
        {
            redirect = Url.Content("~/")!;
        }
        return SignOut(
             new AuthenticationProperties
             {
                 RedirectUri = redirect,
             },
             CookieAuthenticationDefaults.AuthenticationScheme,
             scheme);
    }

深入研究 Microsoft 文檔一個多月后,我終於找到了這個文檔。

Microsoft Docs - OIDC 發送注銷請求

我實際上已經閱讀了這個文檔幾次,然后今天我終於注意到了一個小星號。

客戶 ID 要求

到目前為止,我只在我的 get 請求的查詢字符串中發送了必需的參數。 事件認為客戶端 ID 被標記為不需要,在我的情況下,因為我正在使用應用程序隔離 SSO,並且在注銷請求時需要 ID 令牌設置為“否”。

我通過將 client_id 查詢參數添加到注銷 uri 來更改我的重定向 uri。 我的最終注銷操作如下所示。

public IActionResult SignOut([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);
            }
        }
        var redirectUrl = Url.Content($"https://mytenantname.b2clogin.com/mytenantname.onmicrosoft.com/myuser_flow_policy_name/oauth2/v2.0/logout?post_logout_redirect_uri=https://localhost:44370/&client_id=my_app_registraion_client_guid");
        var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
        
        return SignOut(properties, scheme);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM