I hope this is the solution you are looking for
in startup.cs
in Configure method add the below code
app.UseSession();
in ConfigureServices method add the below code
services.AddDistributedMemoryCache();
services.AddSession(x=>
{
x.Cookie.Name = "Cookie name";
x.IdleTimeout = TimeSpan.FromMinutes(5); // idle time for the session
});
I creating the session in the class file example : UserLogin.cs
private ISession session => httpContext.Session;
public UserLogin(HttpContext httpContext)
{
this.httpContext = httpContext;
}
private void SetSession(ClassInstance ObjOutput)
{
session.SetString("SessionID", ObjOutput.strSession.ToString());
}
in the above code i have injected the HttpContext to class and strSession is the GUID which i will get it from SP,
To validate the session in the api, create the Action filter, In that filter you can get the session in the from context of the OnActionExecuting(ActionExecutingContext context) method
context.HttpContext.Request.Headers["Session"]; this line will get the session from header
context.HttpContext.Session.GetString("SessionID"); this line will get the current session
if it both matches it is okay if not
you can use the below code to tell session expired
string strExceptionOutput = JsonConvert.SerializeObject(new response()
{
StatusCode = (int)HttpStatusCode.InternalServerError,
message = "Session Expired"
});
response.ContentType = "application/json";
context.Result = new BadRequestObjectResult(strExceptionOutput);