![](/img/trans.png)
[英]Exception: The oauth state was missing or invalid. (ASP.NET Core external identifier OAuth)
[英]ASP.NET Core OAuth2 implementation using company provider. Exception: The oauth state was missing or invalid
我正在 ASP.NET Core(使用 .NET 5、Razor Pages)應用程序中實現 OAuth。 我遇到了一個錯誤,但我不太明白它在告訴我什么。
我還沒有讓 OAuth 實現工作,所以即使我能夠讓它工作並解決這個錯誤,我也不相信我已經完美地設置了一切。 如果您對我可以改進的事情有任何建議,將不勝感激!
作為參考,我只嘗試使用 OAuth 幾天,並且我之前沒有任何身份驗證/授權經驗。 不過,我已經閱讀了一些指南,我會在我的帖子末尾提到這些。
我已經包含了有關以下內容的詳細信息:
Exception: The oauth state was missing or invalid.
堆棧跟蹤:
System.Exception: An error was encountered while handling the remote login.
---> System.Exception: The oauth state was missing or invalid.
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Startup.cs ConfigureServices()
public void ConfigureServices(IServiceCollection services)
{
// OAuth2 Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "MyCompanyAuthenticationScheme";
}).AddCookie(options =>
{
options.Cookie.IsEssential = true;
}).AddOAuth("MyCompanyAuthenticationScheme", options =>
{
options.ClientId = Configuration["Company:ClientId"];
options.ClientSecret = Configuration["Company:ClientSecret"];
options.CallbackPath = new PathString("/Auth/Login");
options.AuthorizationEndpoint = "...";
options.TokenEndpoint = "...";
options.UserInformationEndpoint = "...";
// todo: are all three of these necessary?
options.Scope.Add("loginAccountInfo");
options.Scope.Add("contactInfo");
options.Scope.Add("employeeInfo");
options.SaveTokens = true;
// todo: are the jsonKeys defined in the above Scopes? follow up when you have a better understanding of how these work, or if the MapJsonKey lines are even necessary
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "firstName");
options.ClaimActions.MapJsonKey(ClaimTypes.Email, "emailAddress");
});
}
Startup.cs 配置()
app.UseAuthentication()
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
驗證控制器:
[AllowAnonymous]
[Route("[controller]/[action]")]
public class AuthController : Controller
{
[HttpGet]
public IActionResult Login(string returnUrl = "/")
{
if(!User.Identity.IsAuthenticated)
return Challenge(new AuthenticationProperties() { RedirectUri = returnUrl});
return Forbid();
}
}
我認為這是與 OAuth 相關的所有代碼,但如果您認為我需要包含更多詳細信息,請告訴我!
options.CallbackPath
中,在 services.AddOAuth() 下。
options.CallbackPath
。options.CallbackPath
由 OAuth 中間件在內部使用,並且它不對應於控制器操作(這意味着我不需要我的 Auth 控制器及其登錄操作)。 但是,如果我刪除定義options.CallbackPath
的行,我會收到另一個錯誤,指出它未定義,這讓我相信這是非常必要的。options.CallbackPath = new PathString("/Auth/Login");
如果您有一個綁定到/Auth/Login
的控制器操作,這應該是問題的根源。
options.CallbackPath
的值不必匹配任何控制器操作。 它是中間件接收身份提供者答案的內部路由。 您可以簡單地將/callback
指定為其值或與現有路由不匹配的任何其他值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.