0

I have a web api controller which retrieves data from my database and a mvc web app controller which consumes this web api. In my web api I used Microsoft.Owin for security and generating a token. Later, I am using this token taken from my web api via postman, and placing it statically on my web app. What I want to do is dynamically store every token I generate from every request and not copy-pasting it every time. I used this video for creating my web api and generatin jwt token, and this video to consume my web api. Please help me, I'm stuck here for days now.

EDIT:

My web api controller:
        [Authorize]
        [HttpGet]
        public IHttpActionResult GetOperators()
        {
            IList<OperatorClass> OperatorObject = myEntity.Operator.Include("Operator").Select(x => new OperatorClass()
            {   id = x.id,
                name = x.name,
                lastname = x.lastname,
                mobile = x.mobile,
                username = x.username,
                password = x.password
            }).ToList<OperatorClass>();
            return Ok(OperatorObject);
        }

Startup.cs

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        var myProvider = new MyAuthorizationServerProvider();
        OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(5),
            Provider = myProvider
           
        };

MyAuthorizationServerProvider.cs

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        if(context.UserName=="test" && context.Password == "test")
        {
            context.Validated(identity);
        }
        else
        {
            context.SetError("Invalid grant", "Provided username or password are incorrect");
            return;
        }
    }

My web application controller

if (opc.username == "test" && opc.password == "test")
            {
                string token = "07Jv8mQ-pg6MlGdAAVJqxzsJ";

                IEnumerable<OperatorClass> OperatorObject = null;
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("https://localhost:44304/api/");
                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

                var resultDisplay = await client.GetAsync("data");
                if (resultDisplay.IsSuccessStatusCode)
                {
                    var readTable = resultDisplay.Content.ReadAsAsync<IList<OperatorClass>>();
                    readTable.Wait();
                    OperatorObject = readTable.Result;
                    return View(OperatorObject);
                }
                else
                {
                    OperatorObject = Enumerable.Empty<OperatorClass>();
                    ModelState.AddModelError(String.Empty, "No records found");
                    ViewBag.Error = "Token error. It may be incorrect or it has already expired. Check your token provider!";
                    return View("Error");
                }
            }
            else
            {
                ViewBag.Error = "Incorrect username or password!";
                return View("Login");

            }

1 Answers1

0

1- Why don't you store that token in database tables or Session for later use in request. 2- Everytime you ask question try to share relevant code as well next time.

Nauman
  • 218
  • 1
  • 3
  • 11
  • Did you saw the first video? It is because I used a plugin to generate a token and now I can't see or access that token. The only way to see it it's by postman when I call it by giving some user credentials. –  Aug 25 '21 at 13:31
  • Its not possible for us to watch 45 minutes videos – Nauman Aug 25 '21 at 13:32
  • use break points to know where the token is generated everytime and then save it in sessions or database table. – Nauman Aug 25 '21 at 13:34
  • https://stackoverflow.com/questions/34674707/how-to-store-bearer-tokens-when-mvc-and-web-api-are-in-different-projects Follow this. :) – Nauman Aug 25 '21 at 13:44
  • Looks like we have the same problem but even him couldn.t find any solution yet... But thank you though, I will try and ask him personally if he has found any solution to this. –  Aug 25 '21 at 14:01
  • Did you debug your code? try to use break points to find where the token gets generated in your code. – Nauman Aug 25 '21 at 14:03
  • Yes I have tried using breakpoints but the thing is that I am using a template which has some built-in functions and now I can't access neither token or something else... –  Aug 25 '21 at 14:08
  • break points needs to put on the server side not on client side. – Nauman Aug 25 '21 at 15:39