Home > Net >  . Netcore3.1 webapi get HttpContext. Returns null when the Session
. Netcore3.1 webapi get HttpContext. Returns null when the Session

Time:01-20

Everybody is good, I use the vue + project netcore3.1 webapi, in addressing the login authentication code, run into problems of a more eggs, generate a verification code, and stored in the session, call directly with swagger landing interface, can get to the session, but if you run the vue to run, HttpContext. Session returns null, and the nonsense not much said, directly on the code,
Startup:
 
Public class Startup
{
Public IConfiguration Configuration {get; }
Public static ILoggerRepository repository {get; set; }
Public Startup (IConfiguration configuration)
{
The Configuration=Configuration;

The repository=that the LogManager. CreateRepository (" Bull. Api ");
//specify the configuration file
The XmlConfigurator. Configure (repository, new the FileInfo (" log4net. Config "));
}



Public void ConfigureServices (IServiceCollection services)
{
Services. AddCors (options=& gt;
{
Options. AddPolicy (" aa ",
Builder=& gt; Builder. AllowAnyOrigin ()
WithMethods (" GET ", "POST", "the HEAD", "PUT", "DELETE", "OPTIONS")
);
});
Services. AddSession (options=& gt;
{
Options. IdleTimeout=TimeSpan. FromMinutes (30);
Options. Cookies. HttpOnly=true;
});
Services. AddControllers (options=& gt;
{
Options. Filters. Add (a);
})
AddControllersAsServices ()
AddNewtonsoftJson (options=& gt;
{
Options. SerializerSettings. ContractResolver=new DefaultContractResolver ();
});
Services. AddScoped ()
. AddTransient ()
. AddSingleton (Configuration)
. AddSingleton ()
AddLogging ()
. Configure (options=& gt;
{
Options. AllowSynchronousIO=true;
})
. Configure (options=& gt;
{
Options. AllowSynchronousIO=true;
})
//. Configure (options=& gt;
//{
//options. CheckConsentNeeded context of==& gt; False;//Default is true, make it false
//})
AddSwaggerGen (c=& gt;
{
C.s. waggerDoc (" v1 ", new OpenApiInfo
{
Version="v1.0.0,"
Title="interface document"
});
//JWT certification
C.a. ddSecurityDefinition (" Bearer ", new OpenApiSecurityScheme
{
The Scheme="bearer",
JWT BearerFormat=", "
Type=SecuritySchemeType. Http,
Name="Authorization,"
In=ParameterLocation Header,
Description="Authorization: Bearer {your JWT token} & lt; br/> Authorized address:/Base_Manage/Home/SubmitLogin" ,
});
C.a. ddSecurityRequirement (new OpenApiSecurityRequirement
{
{
New OpenApiSecurityScheme {Reference=new OpenApiReference
{
Type=ReferenceType SecurityScheme,
Id="Bearer"
}
},
New string [] {}
}
});
//to Swagger JSON and the XML documentation comments UI path
Var basePath=Path. GetDirectoryName (typeof (Program). The Assembly. The Location).//get the application directory (absolute, not affected by the working directory, suggest using this method to obtain path)

//base layer
C.I ncludeXmlComments (Path.Com bine (basePath, "Coldairarrow. Util. XML"));

//physical layer
C.I ncludeXmlComments (Path.Com bine (basePath, "Coldairarrow. Entity. XML"));

//the business logic layer
C.I ncludeXmlComments (Path.Com bine (basePath, "Coldairarrow. Business. XML"));

//the controller layer
C.I ncludeXmlComments (Path.Com bine (basePath, "Coldairarrow. Api. XML"), true);
});
//services. AddDistributedMemoryCache (.) AddSession ();
Services. AddDistributedMemoryCache ();
Services. AddMvc ();
}

Public void ConfigureContainer (ContainerBuilder builder)
{
//add the service registry here
Var baseType=typeof (IDependency);

//automatic injection IDependency interface, support AOP, life cycle for InstancePerDependency
Var diTypes=GlobalData. FxAllTypes
The Where (x=& gt; BaseType. IsAssignableFrom (x) & amp; & X!!!=baseType)
The ToArray ();
Builder. RegisterTypes (diTypes)
AsImplementedInterfaces ()
PropertiesAutowired ()
InstancePerDependency ()
EnableInterfaceInterceptors ()
InterceptedBy (typeof (Interceptor));

//register Controller
Builder. RegisterAssemblyTypes (typeof (Startup). GetTypeInfo (). The Assembly)
The Where (t=& gt; Typeof (Controller). IsAssignableFrom (t) & amp; & T.N ame. EndsWith (" Controller ", StringComparison. Ordinal))
PropertiesAutowired ();

//AOP
Builder. RegisterType (a);
}

Public void the Configure (IApplicationBuilder app, IWebHostEnvironment env)
{
App. UseCookiePolicy ();
App. UseSession ();//the session on the back is an error, do not know why
//Request Body reuse
App. Use (next=& gt; The context=& gt;
{
The context. Request. EnableBuffering ();

Return next (context);
})
. UseMiddleware (a)//cross-domain
UseDeveloperExceptionPage ()
UseStaticFiles (new StaticFileOptions
{
ServeUnknownFileTypes=true,
DefaultContentType="application/octet - stream"
})
//Swagger configuration
UseSwagger ()
UseSwaggerUI (c=& gt;
{
C.s. waggerEndpoint ("/swagger/v1/swagger. Json ", "1.0.0");
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related