selvi jp

selvi jp

  • NA
  • 323
  • 69.7k

how to display username after login in web api

Apr 28 2021 7:22 AM
This is my login MVC code I just call api login here.I want to display Login Username after login Home Page
 
[AllowAnonymous]
        //[ValidateAntiForgeryToken]
        [HttpPost]
        public async Task<ActionResult> Login(Login model, string returnUrl)
        {
            /*This will depend totally on how you will get access to the identity provider and get your token, this is just a sample of how it would be done*/
            /*Get Access Token Start*/
            HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri("https://localhost:44327/Token");
            var postData = new List<KeyValuePair<string, string>>();
            postData.Add(new KeyValuePair<string, string>("UserName", model.UserName));
            postData.Add(new KeyValuePair<string, string>("Password", model.Password));
            postData.Add(new KeyValuePair<string, string>("grant_type", "password"));
            HttpContent content = new FormUrlEncodedContent(postData);
            

            HttpResponseMessage response = await httpClient.PostAsync("https://localhost:44327/Token", content);
            // response.EnsureSuccessStatusCode();
            string AccessToken = response.Content.ReadAsStringAsync().Result;
            //string AccessToken = /*Newtonsoft.Json.*/JsonConvert.DeserializeObject<string>(result);
            /*Get Access Token End*/
            
            if (AccessToken.Length >= 100)
            {
                TempData["userid"] = "userid";
                return RedirectToAction("Home");
            }
            
            ModelState.AddModelError("UserName", "Email and password is not valid");
            return View(model);
        }

Answers (2)