通过Ajax

时间:2015-05-14 08:26:45

标签: javascript ajax openid openid-connect identityserver3

普通的OpenIDConnect服务器的工作方式如下:

  • 您转到a.com/secure-resource
  • 您从服务器获得302
  • 您的浏览器处理它并将您发送到身份服务器
  • 您登录
  • 它会通过a.com
  • 将您发回POST
  • 您已登录a.com并在浏览器上重新获得a.com/secure-resource

然而我有一个我想要解决的方案,但我需要你的帮助。

  • 用户已登录idServer
  • 用户已登录a.com
  • 用户未登录b.com
  • 我们需要向网络服务器b.com发送ajax调用(来自其他域a.com
  • b.com配置为使用OpenIDConnect。
  • 但由于对b.com的请求是通过Ajax进行的,因此用户无法正常重定向到idServer。 (我们得到的回应是302

我们可以继续通过Ajax处理302(我仍然不确定这是否可行,安全性方面)。

BUT

IdentityServer / OpenIDConnect中是否有针对这些情况设计的场景?

1 个答案:

答案 0 :(得分:3)

在此方案中使用IdentityServer设置服务器b.com以使用承载令牌身份验证,然后您需要在Ajax调用的标头中使用为a.com提供的访问令牌

$.ajax({
     url: 'http://b.com',
     headers: {
          Authorization: "Bearer " + Your Access Token
         }
     })

JavaScript IdentityServer客户端示例具有从Identity Server检索令牌的方法,see here

在控制器中,您可以像这样获取用户和令牌

// Get the claims values
var token= (User as ClaimsPrincipal).Claims
               .Where(c => c.Type == "access_token")
               .Select(c => c.Value).SingleOrDefault();

在应用程序的其他部分,您可以使用此

//Get the current claims principal
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;

// Get the claims values
var token = identity.Claims.Where(c => c.Type == "accept_token")
               .Select(c => c.Value).SingleOrDefault();