2017-08-10 61 views
1

我构建了一个向IdentityServer4发送身份验证详细信息(用户名/密码)并成功接收令牌(授权类型=资源所有者密码)的Angular 4站点。IdentityServer4 Cookie验证

现在我想从Angular 4站点将用户重定向到另一个(不同的)ASP Core MVC站点。 不知何故,我找不到在重定向中设置“标题”以将令牌传递为授权的方式:承载xxx

我设置的cookie在角部位,然后就window.location.href = 'www.example.com/another_site

在MVC网站,我配置Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
     ... 
     ... 

     app.UseCookieAuthentication(new CookieAuthenticationOptions() 
     { 
      AuthenticationScheme = "Cookies_Authentication", 
      AutomaticAuthenticate = true, 
      AutomaticChallenge = true 
     }); 

     app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     { 
      Authority = "http://my_identityserver4_address:49950", 
      RequireHttpsMetadata = false, 
      AuthenticationScheme = "Cookies_Authentication", 

      ApiName = "Protected_Api.get", 
      AutomaticAuthenticate = true, 
      AutomaticChallenge = true 
     }); 
} 

,但我还是收到从MVC站点401未授权的响应。我做错了什么?

可能是IdentityServer寻找特殊的cookie名称?我将它设置在角为:

cookieService.setItem('access_token', 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImFlOWEyMzNhZDczOTUwNWI4YjJkZGE0NTNiMDE........)

回答

1

我想你可能会发现你的答案here只是看到应用程序接收来自服务器的身份令牌如何MVC和从您的角度应用重定向时,只是做同样的事情。你需要玩这个example

UPDATE:@jasper是你的权利,你需要在https://identityserver4.readthedocs.io检查OAuth的OpenID的事情码头也结帐在http://leastprivilege.com

+0

我想你的答案是最接近的 - 我需要更多地了解[OAuth和OpenID Connect](https://identityserver4.readthedocs.io/en/release/quickstarts/3_interactive_login.html),但方向是使用OpneID Connect用于cookie身份验证,而不是承载头。 – Jasper

0

自己的博客如果另一个MVC应用程序时,你不要在auth服务器上定义为另一个客户端”除了在启动类中配置cookie和身份验证中间件之外,您还必须设置其他任何设置,就像您已经在做的那样。

基本上,如果用户从App A登录并打开另一个App B,则不需要重新登录,因为它可以验证浏览器上的现有Cookie。它只需要通过反向通道验证自己的身份。

+0

你是说什么意思:...它只需要验证自己...? – Jasper

+0

我的意思是用户已经被授权,因为如果用户登录App A并打开App B,但用户没有通过后台通道进行认证,因为它已经获得认证代码。身份服务器为您提供单点登录,用户无需在每个应用程序上重新登录。但访问令牌会因不同的应用程序而有所不同。 – JayDeeEss