gpt4 book ai didi

authentication - 如何使用 Owin 安全性从针对 WebAPI 的自动化测试中进行身份验证?

转载 作者:行者123 更新时间:2023-12-02 03:20:30 27 4
gpt4 key购买 nike

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

启动时:

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 的请求 header 中的不记名 token 。

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

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

最佳答案

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

所以你最好的选择似乎是使用 authorization code授予对测试用户进行身份验证,从 token 端点响应中提取刷新 token 。然后使用刷新 token 、客户端 ID 和密码从集成测试中获取访问 token 。

有关更多信息,请参阅: https://developers.google.com/identity/protocols/OAuth2InstalledApp

关于authentication - 如何使用 Owin 安全性从针对 WebAPI 的自动化测试中进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com