refresh token

mpiedra
Community Member

Hi everyone I am using core 3.1 to connect to the canvas API, right now I have my token expire so I have to refresh, and I have problem with that.

 services.AddAuthentication(config =>{
                config.DefaultAuthenticateScheme = "CanvasCookies";
                config.DefaultSignInScheme = "CanvasCookies";
                config.DefaultChallengeScheme = "CanvasLMS";
            })
                .AddCookie("CanvasCookies")
                .AddOAuth("CanvasLMS", config => 
                {
                    var canvas_domain = Configuration.GetValue<string>("Canvas:Domain");
                    var client_secret = Configuration.GetValue<string>("Canvas:Secret");
                    var client_id = Configuration.GetValue<string>("Canvas:Client_id");

                    config.ClientId = client_id;
                    config.ClientSecret = client_secret;
            

                    config.CallbackPath = new PathString("/oauth/callback");
                    config.AuthorizationEndpoint = $"{canvas_domain}login/oauth2/auth";
                    config.TokenEndpoint = $"{canvas_domain}login/oauth2/token";
                    config.SaveTokens = true;
                    config.Events = new OAuthEvents()
                    {
                        OnAccessDenied = async context =>
                        {
                            Console.WriteLine($"***** Access Denied: {context.AccessDeniedPath.Value}");

                        },
                        OnRemoteFailure = async context =>
                        {
                            Console.WriteLine($"***** Failure: {context.Failure.Message}");
                            Console.WriteLine($"***** StackTrace: {context.Failure.StackTrace}");
                        },
                        OnCreatingTicket =  context =>
                        {
                            var accessToken = context.AccessToken;
                            var base64payload = accessToken.Split('.')[1];
                            var bytes = Convert.FromBase64String(base64payload);
                            var jsonPayload = Encoding.UTF8.GetString(bytes);
                            var claims = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonPayload);

                            foreach(var claim in claims)
                            {
                                context.Identity.AddClaim(new Claim(claim.Key, claim.Value));
                            }

                            return Task.CompletedTask;
                        }

 

I need to access to all the course, I am obtain this when I execute

 

Exception: The oauth state was missing or invalid.

Unknown location

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

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler<TOptions>.HandleRequestAsync()

 

Any idea??

 

Thanks!

Labels (3)
0 Likes