oauth状态丢失或无效.处理远程登录时遇到错误 [英] The oauth state was missing or invalid. An error was encountered while handling the remote login

查看:32
本文介绍了oauth状态丢失或无效.处理远程登录时遇到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不使用身份的情况下在asp.net core 2.2(mvc)中实现外部登录时遇到了问题.登录到Google后,它将重定向回下图所示的引发异常的回调URL.

异常:oauth状态丢失或无效.

未知位置

异常:处理远程登录时遇到错误.

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()

有关我所做的更详细的步骤,

下面是Startup.cs设置

  public void ConfigureServices(IServiceCollection服务){services.Configure< CookiePolicyOptions>(options =>{//此lambda决定对于给定的请求是否需要用户同意非必要的cookie.options.CheckConsentNeeded =上下文=>真的;options.MinimumSameSitePolicy = SameSiteMode.None;});服务.AddAuthentication(options =>{options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;}).AddCookie(options =>{options.Cookie.IsEssential = true;}).AddGoogle(options =>{options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;options.ClientId =配置["Authentication:Google:ClientId"];options.ClientSecret =配置["Authentication:Google:ClientSecret"];options.CallbackPath ="/externallogincallback";});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);} 

下面是我的HomeController.cs设置

 //对Google登录名发出挑战的操作公共IActionResult Google(字符串提供程序){provider ="Google";//向外部登录中间件发出挑战以触发登录过程//返回新的ChallengeResult(provider);var authenticationProperties =新的AuthenticationProperties{RedirectUri = Url.Action("externallogincallback")};返回质询(authenticationProperties,"Google");}//回调操作以检索登录用户详细信息[HttpGet("externallogincallback",Name ="externallogincallback")][AllowAnonymous]公共Task< IActionResult>externallogincallback(字符串returnUrl = null,字符串remoteError = null){//这里我们可以检索声明var result = HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);返回null;} 

在Google控制台设置中授权的重定向URI

用于来自Web服务器的请求.这是用户通过Google身份验证后将重定向到您的应用程序中的路径.该路径将附加授权码以进行访问.必须有一个协议.不能包含URL片段或相对路径.不能是公共IP地址.

 <代码> https://localhost:44379/externallogincallback 

解决方案

从设置中删除回调路径

  options.CallbackPath ="/externallogincallback"; 

I am facing issues while implementing external login in asp.net core 2.2 (mvc) without using identity. After signing in to google it redirect back to callback url that is throwing exception as attached in the image below.

Exception: The oauth state was missing or invalid.

Unknown location

Exception: An error was encountered while handling the remote login.

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()

For more detailed steps that I did, please check here

Below is Startup.cs settings

 public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services
            .AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
            })
            .AddCookie(options =>
            {
                options.Cookie.IsEssential = true;
            })
            .AddGoogle(options =>
            {
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.ClientId = Configuration["Authentication:Google:ClientId"];
                options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                options.CallbackPath = "/externallogincallback";

            });

              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

Below is my HomeController.cs settings

 //Action to issue a challange to google login
    public IActionResult Google(string provider)
    {
        provider = "Google";
        //Issue a challenge to external login middleware to trigger sign in process
        //return new ChallengeResult(provider);

        var authenticationProperties = new AuthenticationProperties
        {
            RedirectUri = Url.Action("externallogincallback")
        };          

        return Challenge(authenticationProperties, "Google");
    }

    //Callback action to retrive signin user details
    [HttpGet("externallogincallback", Name = "externallogincallback")]
    [AllowAnonymous]
    public Task<IActionResult> externallogincallback(string returnUrl = null, string remoteError = null)
    {
        //Here we can retrieve the claims
        var result =  HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);

        return null;
    }

In Google console setting Authorized redirect URIs

For use with requests from a web server. This is the path in your application that users are redirected to after they have authenticated with Google. The path will be appended with the authorization code for access. Must have a protocol. Cannot contain URL fragments or relative paths. Cannot be a public IP address.

https://localhost:44379/externallogincallback 

解决方案

Remove callbackpath from setting

options.CallbackPath = "/externallogincallback";

这篇关于oauth状态丢失或无效.处理远程登录时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆