Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

HttpCookie to Session Cookie

$
0
0

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.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>