Rayrun
← Back to Discord Forum

Using C# - after doing a Login Api call, how do I then make subsequent API calls. StorageState?

I login to my API like this:

var apiResponse = await this.Request.PostAsync("SignInPwd?submit=submit", new APIRequestContextOptions { Form = formData });

I was hoping that i could either just reuse this.Request to make more GetAsync or PostAsync calls or do this:

var storageState = await this.Request.StorageStateAsync();
        Console.WriteLine($"StorageState: {JsonConvert.SerializeObject(storageState)}");
        var loggedInContext = await Playwright.APIRequest.NewContextAsync(
            new APIRequestNewContextOptions
            {
                // All requests we send go to this API endpoint.
                BaseURL = Environment.GetEnvironmentVariable("ApiBaseUrl"),
                StorageState = storageState,
                IgnoreHTTPSErrors = true
            });
        
        var accountUserResponse = await loggedInContext.GetAsync("api/accountapi/accountuser");

And use the loggedInContext but sadly, i'm still logged out.

What am I doing wrong? Auth is in the cookies.

This thread is trying to answer question "How can I maintain an authenticated session after logging in to an API to make subsequent API calls in C#?"

1 reply

BTW. Yes I do set the base Url for the this.Request in [SetUp] methods.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].