Would like to convert the following lines of code:
private const string USERCOOKIENAME = "mysite.user";
protected virtual HttpCookie GetUserCookie()
{
if (_httpContext == null || _httpContext.Request == null)
return null;
return _httpContext.Request.Cookies[USERCOOKIENAME];
}
protected virtual void SetUserCookie(Guid userGuid)
{
if (_httpContext != null || _httpContext.Response != null)
{
var cookie = new HttpCookie(USERCOOKIENAME);
cookie.HttpOnly = true;
cookie.Value = userGuid.ToString();
if (userGuid == Guid.Empty)
{
cookie.Expires = DateTime.Now.AddMonths(-1);
}
else
{
int cookieExpires = 24 * 365;
cookie.Expires = DateTime.Now.AddHours(cookieExpires);
}
_httpContext.Response.Cookies.Remove(USERCOOKIENAME);
_httpContext.Response.Cookies.Add(cookie);
}
}
I already have this in my Startup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions { LoginPath = "/Login", AccessDeniedPath = "/Error", AuthenticationScheme = "Cookies", AutomaticAuthenticate = true, AutomaticChallenge = true });
I use my own Users/Roles tables.