【发布时间】:2021-01-17 15:25:36
【问题描述】:
我使用 Dotnetcore 3.1 创建了一个 Web 应用程序。使用 OAuth 2 方法通过 API 进行身份验证。
我需要找到一种方法来获取 API 返回的 URL 中的代码。
在我的项目中,Startup.cs 如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "MyAuthSchema";
})
.AddCookie()
.AddOAuth("MyAuthSchema", options =>
{
options.ClientId = Configuration["artAPI:ClientId"];
options.ClientSecret = Configuration["artAPI:ClientSecret"];
options.CallbackPath = new PathString("/test");
options.AuthorizationEndpoint = "https://artForce.ch/oauth/authorize?response_type=code&client_id=y_gkjtj448HGHG483&redirect_uri=https://display.zh.edu/authorize";
options.TokenEndpoint = "MyTokenEndPoint";
options.UserInformationEndpoint = "MyUserInformationEndPoint";
});
}
第 3 方 API 希望端点的格式如下所示:
options.AuthorizationEndpoint = "https://artForce.ch/oauth/authorize?response_type=code&client_id=y_gkjtj448HGHG483&redirect_uri=https://display.zh.edu/authorize";
https://artForce.ch/oauth/authorize 是第 3 方 API 端点
https://display.zh.edu/authorize 是我的应用程序。
我可以使用它,但我希望能够在我的代码中获取授权令牌,然后再将我重定向到 https://display.zh.edu/authorize。
所以当我尝试授权时,我按下一个按钮来触发这个控制器:
[Route("[controller]/[action]")]
public class AuthController : Controller
{
[HttpGet]
public IActionResult Login(string returnUrl = "/")
{
return Challenge(new AuthenticationProperties() { RedirectUri = returnUrl });
}
}
但是当它到达return Challenge 行时,它会重定向我,我无法弄清楚我事先要捕获授权码。
重定向到这里:
https://display.zh.edu/authorize?code=JsAt9KwROG_19484846333GFHsuuwuh
这就是我想要在 重定向之前得到的:
code=JsAt9KwROG_19484846333GFHsuuwuh
我什至在其中设置了一个断点并检查了 Visual Studio 中的所有本地变量和自动变量,但没有显示任何类型的授权代码。
所以我想知道,是否有什么地方可以在我的程序中获取该代码,然后再进行重定向?
谢谢!
【问题讨论】:
标签: c# asp.net-core oauth-2.0 asp.net-core-3.1 c#-8.0