c# - ASP.NET Core authentication cookie only received once -
i developing application asp.net core , using custom cookie authentication. cookieauthenticationoptions
are:
app.usecookieauthentication(new cookieauthenticationoptions() { authenticationscheme = cookieauthenticationdefaults.authenticationscheme, loginpath = new pathstring("/login"), accessdeniedpath = new pathstring("/unauthorized/"), automaticauthenticate = true, automaticchallenge = true });
the cookie created fine , can see in browser settings throughout whole time running application. homecontroller
class:
public homecontroller(ihostingenvironment env, iantiforgery antiforgery, ioptions<appsettings> appsettings, terminaldbcontext terminalcontext, iloggerfactory loggerfactory, ihttpcontextaccessor _httpcontextaccessor) { _env = env; _antiforgery = antiforgery; _appsettings = appsettings; _terminalcontext = terminalcontext; _logger = loggerfactory.createlogger<homecontroller>(); _httpcontext = _httpcontextaccessor.httpcontext; _logger.loginformation("cookie coming"); var cookies = _httpcontext.request.cookies[".aspnetcore.cookies"]; if (cookies != null) { _logger.loginformation(cookies.length.tostring()); _logger.loginformation(cookies.tostring()); } else { _logger.loginformation("the cookie null"); } }
and how sign in user:
var claims = new list<claim> { new claim(claimtypes.name, logininfo.username), new claim("dbname", logininfo.terminal.sesamdbname), }; var useridentity = new claimsidentity(claims, "password"); claimsprincipal principal = new claimsprincipal(useridentity); await _httpcontext.authentication.signinasync(cookieauthenticationdefaults.authenticationscheme, principal);
i running application , more 1 instances of homecontroller
created, since have httpget
methods return jsonresult
needed view.
the first time application tries [authorize]
(for index()
method), finds cookie , authenticates , authorizes fine. second time tries [authorize]
(for httpget
method returns jsonresult
) doesn't find cookie, though there in browser's settings. log get, illustrate this:
... info: server.controllers.homecontroller[0] cookie coming info: server.controllers.homecontroller[0] 347 info: server.controllers.homecontroller[0] cfdj8gslzenxanpnrtmz2dat9joqj6cehpcfbjdbnxbqyjjoqmd4naoi0l0krnmsqdvhqprp9tjjmmirayc5ilrqmcjqwnz0t9fjuk7qxg65wpp7sr43uzxwy6vgq7_qesp44gylle4ngealhxynzxmd-jywql4vjz5y4owpseklx-vvt03xalt54j_qqk_o4wjwlqizbpavtfkuwn4u7h8yd_rwmtigbpu21t5n35to9btqu5677xnxiefap3ukuxo4p-oxvakxqshy2xk_vydavv_xfv6jgncy4zicrb8vuhxgcnr205h4x0-o7jhb8mybc13azlmrawvg5dwtbd3_oco ... info: server.controllers.homecontroller[0] cookie coming info: server.controllers.homecontroller[0] cookie null
why happen? can it?
the issue had nothing backend. using react in front-end , problem fetch()
not passing cookies back-end get
methods. had set { credentials: 'same-origin' }
fetch()
in order send cookies request. help.
Comments
Post a Comment