Click here to Skip to main content
14,241,248 members

How to manage/use the token returned (on successful login) by a webapi provider to call/consume their other (authorized)web apis?

Member 11274606 asked:

Open original thread
Hi,
I am trying to add online appointment booking functionality to an existing website which is built using Umbraco. And using a third party web api service to implement this booking functionality. This web-api provider also handles the login part to authenticate the user and returns the XSRF-TOKEN which i need to use(implicitly) to call their other APIs. So far i am able to get this token and add it to DefaultRequestHeaders (inside the same controller)like:
public class AccountController : SurfaceController
   {
       HttpClient client;
       CookieContainer cookies = new CookieContainer();
       string token;
       public  AccountController()
       {
           HttpClientHandler handler = new HttpClientHandler();
           handler.CookieContainer = cookies;
           client = new HttpClient(handler);
           client.BaseAddress = new Uri("url");
           client.DefaultRequestHeaders.Accept.Clear();
           client.DefaultRequestHeaders.Accept.Add(new
           MediaTypeWithQualityHeaderValue("application/json"));
       }

       public async Task<bool> ValidateLogin(LoginModel model)
       {
           HttpResponseMessage responseMessage = await client.PostAsJsonAsync("url", model);
           Uri uri = new Uri("url");
           IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();

           token = responseCookies.FirstOrDefault(x => x.Name == "XSRF-TOKEN").Value;
           if (!String.IsNullOrWhiteSpace(token))
           {
               client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-XSRF-TOKEN", token);
               client.DefaultRequestHeaders.Add("X-XSRF-TOKEN", token);
               return true;
           }
           return false;
       }

       public async Task<ActionResult> GetUserDetails()
       {
           HttpResponseMessage responseMessage = await client.GetAsync("url");
           if (responseMessage.IsSuccessStatusCode)
           {
               MyViewModel model = new MyViewModel();
               var responseData = responseMessage.Content.ReadAsStringAsync().Result;//Getting an unauthorised response back here since the token is not handled as required

               model.UserDetails = JsonConvert.DeserializeObject<MyViewModel>(responseData);
               model.Appointments = await GetAppointmentsByUser();
               return PartialView("_UserDetails", model);
           }
           return CurrentUmbracoPage();
       }

       private Task<object> GetAppointmentsByUser()
       {
          //Code
       }


So now the real problem is how to manage/handle this token so the logged user can get authenticated to use other GET, PUT,POST requests?

Can somebody guide me to implement this in a proper way please?

Thanks

What I have tried:

Tried to find the solution on similar issue but did not get satisfying answer yet.
Tags: C#, MVC, umbraco

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100