I have been stuck on such issue related to cookies.
My scenario is this:
- I have my backend on server (localhst:12456)
- I have my Angular 2 app running on another server (localhost:5555)
My backend is just an ASP.NET application and I am trying to authenticate user from an apiController. the api Controller is like this:
[System.Web.Mvc.HttpPost]
public HttpResponseMessage HandleLogin([FromBody] LoginModel loginModel)
{
if (!String.IsNullOrEmpty(loginModel.Username) && !String.IsNullOrEmpty(loginModel.Password))
{
if (Members.Login(loginModel.Username, loginModel.Password))
{
var resp = Request.CreateResponse<UserAuthenticationModel>(
HttpStatusCode.OK,
new UserAuthenticationModel() { IsAuthenticated = true}
);
//create and set cookie in response
var cookie = new CookieHeaderValue("customCookie", "cookieVal");
cookie.Expires = DateTimeOffset.Now.AddDays(1);
cookie.Domain = Request.RequestUri.Host;
cookie.Path = "/";
resp.Headers.AddCookies(new CookieHeaderValue[] { cookie });
return resp;
}
}
return Request.CreateResponse<UserAuthenticationModel>(
new UserAuthenticationModel() { IsAuthenticated = false }
);
}
Now, from my angular app, I am calling an http post:
headers.append('Access-Control-Allow-Credentials', true);
return this._http.post(this._globalVariables.BACKEND_API_HANDLE_LOGIN, loginModel, {headers: headers})
.map((response : Response) => response);
this._loginService.getLoginModel() .subscribe( loginModel => this._signInPageModel = loginModel, error => console.log(error) ); Now, in my api controller method (HandleLogin) I have a test cookie and another one for FormsAuthentication and when user logged in successfully, and return back to my angular app, no cookies are created.
Now, the cookies can be seen as well as post of my response headers:
I'm quite confusing about it and appreciate any help in order to get the login process successfully.
Thank you in advance.