2017-11-25 324 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"); 
} 
相关问题