1

I have a React page that makes a request to stripe onboarding and stores the relevant data in sessions like this:

[HttpPost]
[Route("SignupSeller")]
public async Task<IActionResult> SignupSeller(SignupSellerInput signupSellerInput)
{
    StripeConfiguration.ApiKey = "xxxx";

    var optionsAccount = new AccountCreateOptions
        {
            Controller = new AccountControllerOptions
                             {
                                 Losses = new AccountControllerLossesOptions { Payments = "application" },
                                 Fees = new AccountControllerFeesOptions { Payer = "application" },
                                 StripeDashboard = new AccountControllerStripeDashboardOptions { Type = "express" },
                                 // RequirementCollection = "application",
                             },
            Capabilities = new AccountCapabilitiesOptions
                           {
                               CardPayments = new AccountCapabilitiesCardPaymentsOptions { Requested = true },
                               Transfers = new AccountCapabilitiesTransfersOptions { Requested = true },
                           },
            Email = signupSellerInput.email,
            BusinessType = "individual",
            Country = signupSellerInput.country,
        };

    var serviceAccount = new AccountService();
    Account account = serviceAccount.Create(optionsAccount);

    var optionsAccountLink = new AccountLinkCreateOptions
        {
            Account = account.Id,
            RefreshUrl = "http://localhost:3000/stripeerror",
            ReturnUrl = "http://localhost:3000/stripecomplete",
            Type = "account_onboarding",
            // CollectionOptions = new AccountLinkCollectionOptionsOptions
            //     {
            //         Fields = "currently_due",
            //     }, 
        };

    var serviceAccountLink = new AccountLinkService();
    AccountLink accountLink = serviceAccountLink.Create(optionsAccountLink);

    HttpContext.Session.SetString("accountId", account.Id);
    HttpContext.Session.SetString("accountEmail", signupSellerInput.email);
    HttpContext.Session.SetString("accountFirstName", signupSellerInput.firstname);
    HttpContext.Session.SetString("accountLastName", signupSellerInput.lastname);
    HttpContext.Session.SetString("accountBusinessName", signupSellerInput.businessname);
    HttpContext.Session.SetString("accountPaswword", signupSellerInput.password);
    HttpContext.Session.SetString("accountCountry", signupSellerInput.country);

    return Ok(new { accountLink = accountLink.Url });
}

When the success return url is called from stripe, I go to the StripeCompleted page which makes a fetch call to the Web API to store the information in my tables but the session objects are all null.

Like this:

[HttpPost]
[Route("StoreStripeAccount")]
public async Task<IActionResult> StoreStripeAccount()
{
    string accountId = HttpContext.Session.GetString("accountId");
    string accountEmail = HttpContext.Session.GetString("accountEmail");
    string firstname = HttpContext.Session.GetString("accountFirstName");
    string lastname = HttpContext.Session.GetString("accountLastName");
    string businessName = HttpContext.Session.GetString("accountBusinessName");
    string password = HttpContext.Session.GetString("accountPaswword");
    string country = HttpContext.Session.GetString("accountCountry");

    if (accountId != null && accountEmail != null)
    {
        String guidUser = Guid.NewGuid().ToString();

        TblUser user =  await userRepository.StoreUser(guidUser, accountEmail, password, firstname, lastname, businessName, country);

        stripeRepository.StoreConnectedAcount(user.Id, accountId);

        HttpContext.Session.Remove("accountId");
        HttpContext.Session.Remove("accountEmail");
        HttpContext.Session.Remove("accountFirstName");
        HttpContext.Session.Remove("accountLastName");
        HttpContext.Session.Remove("accountBusinessName");
        HttpContext.Session.Remove("accountPaswword");
        HttpContext.Session.Remove("accountCountry");

        return Ok(new { success = "accountAdded" });
    }
    else
    {
        return Ok(new { error = "couldNotAddAccount" });
    }
}

I have this in my Program.cs where the session timeout is set to 1 year>:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using SoppingaWebAPI.Models;
using SoppingaWebAPI.Repository.Interface;
using SoppingaWebAPI.Repository.Services;
using System;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDistributedMemoryCache();

builder.Services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromSeconds(31536000);
        options.Cookie.HttpOnly = true;
        options.Cookie.IsEssential = true;
    });

builder.Services.AddCors(options =>
    {
        options.AddPolicy("AllowAll", builder =>
        {
            builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
        });
    });

builder.Services.AddDbContext<SoppingaDbContext>(options =>
            options.UseSqlServer(builder.Configuration.GetConnectionString("DBConnection")));

builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IStripeRepository, StripeRepository>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseDefaultFiles();
app.UseStaticFiles();

app.UseCors("AllowAll");
app.UseHttpsRedirection();

app.UseAuthorization();

app.UseSession();

app.MapControllers();

app.Run();

But all the sessions when trying to retrieve them are all null, where am I going wrong?

8
  • Are you app.UseSession(); ?? Commented Aug 28 at 19:46
  • Yes that line of code is added in the program Cs file, I have updated the full program Cs file Commented Aug 28 at 19:48
  • Are you connecting to the correct database? You are not getting any errors, no data is being returned. What type database are you using? Check the log files of the database to see if you are connecting to the correct database. Commented Aug 28 at 21:18
  • This isn't a dB issue its how I'm trying to use sessions and I'm setting the session values but when I want to retrieve the session values they are all null Commented Aug 28 at 21:23
  • Is this because I'm using a stateless api that I can't use sessions? Commented Aug 28 at 22:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.