skip to Main Content

I get this error

InvalidCastException: Unable to cast object of type ‘Token’ to type ‘Microsoft.AspNetCore.Mvc.IActionResult’

Did I do something wrong? I quite new to programing and this is my first internship. I thought this was going to be a piece of cake but I’ve been literally stuck for days doing research and trying to set up an authentication flow(auth2.0) in C# .NET 6

I’m just so tired at this point,I would appreciate any help…
I have client id, client secret and client signin

        using Newtonsoft.Json;
        using Microsoft.AspNetCore.Mvc;
        using System.Diagnostics;
        using WebApplication1.Models;
        using RestSharp;
        using Microsoft.AspNetCore.Authorization;
        using Microsoft.Extensions.Configuration;
        using System;
        using System.Net.Http;
        using System.Net.Http.Json;
        using System.Threading.Tasks;
        using static System.Net.Mime.MediaTypeNames;
        using System;
        using System.Collections.Generic;
        using System.Net.Http;
        using System.Net.Http.Headers;
        using System.Threading.Tasks;
        using System.Web;
        
        
                public async Task<IActionResult> Index()
                {
                    using (var httpClient = new HttpClient())
                    {
                        string baseAddress = "https://auth.monday.com/oauth2/token";
        
                        string grant_type = "client_credentials";
                        string client_id = "id";
                        string client_secret = "secret";
        
                        var clientCreds = System.Text.Encoding.UTF8.GetBytes($"{client_id}:{client_secret}");
                        httpClient.DefaultRequestHeaders.Authorization =
                          new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(clientCreds));
        
                        var form = new Dictionary<string, string>
                {
                    {"grant_type", grant_type}
                };
        
                        HttpResponseMessage tokenResponse = await httpClient.PostAsync(baseAddress, new FormUrlEncodedContent(form));
                        var jsonContent = await tokenResponse.Content.ReadAsStringAsync();
                        Token tok = JsonConvert.DeserializeObject<Token>(jsonContent);
                        return (IActionResult)tok;
                    }
        
                    return View();
                }

2

Answers


  1. I’m not totally certain what you’re trying to do after reading your question and code, but you’ve been given the reason. The runtime can’t make your ‘Token’ type into a IActionResult type.

    You should look up InvalidCastException in the MS Docs to understand better how casting works.

    When you return ‘(IActionResult)tok’, whatever tok is can’t be forced into being an IActionResult.

    Login or Signup to reply.
  2. tok object is not inherited from IActionResult so that’s why you are getting InvalidCastException.

    pass tok as view data

    using System.Threading.Tasks;
    using System.Web;
        
    public async Task<IActionResult> Index()
    {
                    using (var httpClient = new HttpClient())
                    {
                        string baseAddress = "https://auth.monday.com/oauth2/token";
        
                        string grant_type = "client_credentials";
                        string client_id = "id";
                        string client_secret = "secret";
        
                        var clientCreds = System.Text.Encoding.UTF8.GetBytes($"{client_id}:{client_secret}");
                        httpClient.DefaultRequestHeaders.Authorization =
                          new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(clientCreds));
        
                        var form = new Dictionary<string, string>
                {
                    {"grant_type", grant_type}
                };
        
                        HttpResponseMessage tokenResponse = await httpClient.PostAsync(baseAddress, new FormUrlEncodedContent(form));
                        var jsonContent = await tokenResponse.Content.ReadAsStringAsync();
                        Token tok = JsonConvert.DeserializeObject<Token>(jsonContent);
            ViewData["token"] = tok ;       
                        return View();
                    }
        
        return View();
    }
    

    And in Index view access the token using

    @{ var token = ViewData["token"] as Token;}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search