簡體   English   中英

如何在 thinktecture IdentityServer 中禁用自動登錄

[英]how to disable auto login in thinktecture IdentityServer

我有一個由身份服務器管理授權的 MVC 應用程序。 當我第一次訪問我的網站時,它被重定向到身份服務器登錄頁面,然后我又被重定向到我的網站。

我的問題是,如果我退出身份服務器,當我再次訪問我的網站(使用身份服務器授權)時,我會被重定向到身份服務器,但登錄是自動完成的,允許我訪問我的網站,而無需將用戶/密碼放入身份服務器。

我認為這是因為客戶端中的 cookie 仍然存在(如果我在瀏覽器中手動刪除所有 cookie,則需要用戶/密碼)。

如何禁用自動登錄(強制始終需要用戶/密碼)?

我的啟動客戶端配置如下:

 app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            LoginPath = new PathString("/Home/Logged/"),
            AuthenticationType = "Cookies",
            ExpireTimeSpan = TimeSpan.FromDays(2),
            SlidingExpiration = true,
            CookieName = ".AspNet.MyApp"

        });


        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            ClientId = "MyApp",
            Authority = IS_URL,
            RedirectUri = localHostURL + "/Home/Logged/",
            PostLogoutRedirectUri = localHostURL + "/Account/Login/",
            ResponseType = "code id_token token", 
            Scope = "openid profile read write sampleApi",
            SignInAsAuthenticationType = "Cookies",

            UseTokenLifetime = true,

            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                SecurityTokenValidated = async n =>
                {
                    var nid = new ClaimsIdentity(
                        n.AuthenticationTicket.Identity.AuthenticationType,
                        "given_name",
                        "role");

                    // get userinfo data
                    var userInfoClient = new UserInfoClient(
                        new System.Uri(n.Options.Authority + "/connect/userinfo"),
                        n.ProtocolMessage.AccessToken);

                    var userInfo = await userInfoClient.GetAsync();
                    userInfo.Claims.ToList().ForEach(ui => nid.AddClaim(new Claim(ui.Item1, ui.Item2)));

                    //keep the id_token for logout

                   nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));

                   // add access token for sample API
                   nid.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));

                    // keep track of access token expiration
                    nid.AddClaim(new Claim("expires_at", TimeSpan.FromDays(2).ToString()));

                    // add some other app specific claim
                    nid.AddClaim(new Claim("app_specific", "some data"));

                    n.AuthenticationTicket = new AuthenticationTicket(
                        nid,
                        n.AuthenticationTicket.Properties);
                },
                RedirectToIdentityProvider = n =>
                {
                    if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
                    {
                        var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token");

                        if (idTokenHint != null)
                        {
                            n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
                        }
                    }

                    return Task.FromResult(0);
                }
            }
        });

提前致謝!

要從身份服務器注銷,您需要重定向到結束會話端點。

通常/connect/endsession 只有這樣才能清除身份驗證會話 cookie。

請參閱規范: https : //openid.net/specs/openid-connect-session-1_0.html#RPLogout

在登錄請求/重定向到 idsrv 時,將prompt參數設置為login

OnRedirectToIdentityProvider = n =>
{

    if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.Authentication)
    {    
        n.ProtocolMessage.Prompt = "login";
    }

    return Task.FromResult(0);
}

IdSrv 文檔(見提示)

prompt (optional)

login將顯示登錄 UI,即使用戶已經登錄並且具有有效的會話。

圍繞/authorize請求的 OpenId Connect 規范

prompt=login

授權服務器應該提示最終用戶重新認證。 如果它不能重新驗證最終用戶,它必須返回一個錯誤,通常是 login_required。

我有類似的要求。 不是最優雅的方法,但我通過將 cookie 過期時間減少到 10 秒並關閉滑動過期時間來解決它。 這里的邊緣情況是,如果依賴方在 10 秒內回來,則繞過登錄提示。

我為使用 MVC(非核心)的機密客戶端應用程序與此作斗爭。 我最終解決了這個問題:在ConfigureAuth(IAppBuilder app)中的 Notifications 下添加對新任務的引用: RedirectToIdentityProvider = OnRedirectToIdentityProvider,然后添加任務:

private Task OnRedirectToIdentityProvider(RedirectToIdentityProviderNotification
<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) 
     {             
         // Forces the user to login. 
         if (notification.ProtocolMessage.Prompt == null) 
         { 
             notification.ProtocolMessage.Prompt = "login";                 
         } 
         return Task.FromResult(0); 
     }

暫無
暫無

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

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