2017-11-25 326 views
1

我的標識服務器正在使用identityserver4框架(http://localhost:9000)。我如下在Identity Server上註冊客戶端。無法註銷ASP.NET Core 2應用程序上Identityserver4的OpenIdConnect身份驗證

clients.Add(
    new Client 
    { 
     ClientId = "customer.api", 
     ClientName = "Customer services", 
     AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, 
     RequireConsent = false, 
     AllowAccessTokensViaBrowser = true, 

     RedirectUris = { "http://localhost:60001/signin-oidc" }, 
     PostLogoutRedirectUris = { "http://localhost:60001/signout-callback-oidc" }, 
     ClientSecrets = new List<Secret> 
     { 
      new Secret("testsecret".Sha256()) 
     }, 
     AllowedScopes = new List<string> 
     { 
      IdentityServerConstants.StandardScopes.OpenId, 
      IdentityServerConstants.StandardScopes.Profile, 
      IdentityServerConstants.StandardScopes.Email, 
      IdentityServerConstants.StandardScopes.OfflineAccess, 
      "customerprivatelinesvn.api",       
     }, 
     AllowOfflineAccess = true, 
     AlwaysIncludeUserClaimsInIdToken = true, 
     AllowedCorsOrigins = { "http://localhost:60001" } 
    }); 

這是我的客戶端應用程序(http://localhost:60001)上的身份驗證。

private void AddAuthentication(IServiceCollection services) 
{ 
    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); 

    services.AddAuthentication(options => 
    { 
     options.DefaultAuthenticateScheme = "Cookies"; 
     options.DefaultChallengeScheme = "oidc";  
    }) 
    .AddCookie() 
    .AddOpenIdConnect("oidc", options => 
    { 
     Configuration.GetSection("OpenIdConnect").Bind(options);   
    });  
}  

"OpenIdConnect": { 
    "SignInScheme": "Cookies", 
    "Authority": "http://localhost:9000/", 
    "RequireHttpsMetadata": false, 
    "ClientId": "customer.api", 
    "ClientSecret": "testsecret", 
    "Scope": [ "customerprivatelinesvn.api", "offline_access" ], 
    "CallbackPath": "/signin-oidc", 
    "ResponseType": "code id_token token", 
    "GetClaimsFromUserInfoEndpoint": true, 
    "SaveTokens": true 
    } 

HomeController的客戶端應用程序的

[Authorize] 
public class HomeController : Controller 
{ 
    public IActionResult Index() 
    { 
     return View(); 
    }  
} 

下面是客戶端應用程序的用戶登錄後的餅乾英寸 enter image description here

我嘗試實現signout動作如下

public class AccountController : Controller 
{ 
    public async Task<IActionResult> Signout() 
    { 
     await HttpContext.SignOutAsync("Cookies"); 
     await HttpContext.SignOutAsync("oidc"); 

     return RedirectToAction("Index", "Home");     
    } 
} 

但是當用戶註銷時,它不會調用身份服務器的終端端點。我看着提琴手的流量,沒有要求識別服務器。

enter image description here

我的期望是,當用戶登錄時,它會調用endsession身份服務器的端點,然後重定向到註銷身份服務器的鏈接如下。

enter image description here

enter image description here

我們可以通過調用OwinContext signout

private void LogoutOwin(IOwinContext context) 
     { 
      context.Authentication.SignOut(); 
     } 

但signout方法不能在ASP.NET工作了核心2

做到這一點很容易地在MVC應用程序

注意:我從AJAX帖子調用註銷操作,因爲我的客戶端應用程序是角度爲5的應用程序。

有誰知道如何在ASP.NET Core 2上正確實現登出?

非常感謝。

問候,

凱文

回答

2

現在我可以解決我的問題。

1)返回SignOutResult將調用endsession端點。

2)更改AJAX後提交表單。

public class AccountController : Controller 
{ 
    public IActionResult Signout() 
    { 
     return new SignOutResult(new[] { "oidc", "Cookies" });    
    } 
} 


<form action="/Account/Signout" id="signoutForm" method="post" novalidate="novalidate"> 
    <ul class="nav navbar-nav navbar-right"> 
     <li><a href="javascript:document.getElementById('signoutForm').submit()">Sign out</a></li> 
    </ul> 
</form> 
0

在網絡核心2.0更改您的代碼使用枚舉CookieAuthenticationDefaults和OpenIdConnectDefaults

services.AddAuthentication(options => 
     { 
      options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; 
     }) 
     .AddCookie() 
     .AddOpenIdConnect(SetOpenIdConnectOptions); 


private static void SetOpenIdConnectOptions(OpenIdConnectOptions options) 
{ 
    options.ClientId = "auAuthApp_implicit"; 
    options.Authority = "http://localhost:55379/"; 

    options.SignInScheme = "Cookies"; 
    options.RequireHttpsMetadata = false; 

    options.SaveTokens = true; 
    options.ResponseType = "id_token token"; 
    options.GetClaimsFromUserInfoEndpoint = true; 

} 

和...

public async Task<IActionResult> Logout() 
{ 
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); 
    await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); 

    return RedirectToAction("Index", "Home"); 
} 
相關問題