【问题标题】:How to authenticate from automated tests against WebAPI with Owin security?如何使用 Owin 安全性通过针对 WebAPI 的自动化测试进行身份验证?
【发布时间】:2016-02-26 07:11:36
【问题描述】:

我有一个具有 owin 安全性的 .net web api 应用程序。安全中间件配置了 Google 身份验证(无本地用户名/密码身份验证)和 Oauth 持有者令牌。

在启动中:

public void ConfigureAuth(IAppBuilder app)
{
   ...
   app.UseOAuthBearerTokens(new OAuthAuthnorizationServerOptions {
      TokenEndpointPath = new PathString("/token"),
      AuthorizeEndpointPath = new PathString("/account/authorize"),
      Provider = new ApplicationOAuthProvider("web"),
      ...
   });

   app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions {
      ClientID = "...",
      ClientSecret = "..."
   });
}

从我的客户端应用程序我去

http://myapp.com/account/authorize?client_id=web&response_type=token&redirect_uri=...

这将重定向到用户填写的谷歌登录,然后返回我的应用程序,并使用我剥离的不记名令牌并添加到我的 api 的请求标头中。

我想编写一些集成测试,我的测试调用 API。测试具有测试用户的 google 凭据,但我不确定如何在不打开浏览器和 google 登录屏幕的情况下让测试进行身份验证。我可以用 Selenium 来驱动它,但这似乎很笨拙。

如何以编程方式获取不记名令牌以与我的 API 一起使用,以便我可以模拟我的测试用户?

【问题讨论】:

    标签: authentication asp.net-web-api owin google-authentication


    【解决方案1】:

    在这种情况下,通常您会使用 OAuth2 resource owner password credentials 授权,但 Google does not seem to support that

    因此,您最好的选择似乎是使用authorization code 授权来验证测试用户,从令牌端点响应中提取刷新令牌。然后使用刷新令牌和客户端 ID 和密钥从您的集成测试中获取访问令牌。

    有关详细信息,请参阅此内容: https://developers.google.com/identity/protocols/OAuth2InstalledApp

    【讨论】:

    • 我试过了,但没用。我通过这种方式获得了一个谷歌不记名令牌,我通过授权标头(授权:不记名 xxx),但它重定向到 /authorize/login。我认为问题在于我需要 Owin/.net 不记名令牌 (OAuthAuthorizationServerProvider) 而不是 Google 不记名令牌?
    • 您确定您传递的是正确的 JWT 令牌吗?您应该能够通过将其粘贴到此处的编码文本字段中来查看令牌内部的内容:jwt.io
    • 是的,根据 jwt.io,JWT 没问题 - 这是一个有效的 google 令牌,我可以将它用于 Google API,但不能用于我自己的 API。
    • 检查令牌中的“aud”声明。它应该与您配置的中间件所期望的相匹配。
    猜你喜欢
    • 2017-12-17
    • 2023-03-31
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多