0
    //My login method is as follows, which takes credentials and validates them against a db table entries
    [HttpPost]
    [Route("api/login")]
    public IHttpActionResult Authenticate([FromBody]LoginModel credentials)
    {
        IHttpActionResult result;
        HttpResponseMessage response;
        TokenModel tokenInfo;
        var userId = (from n in _data.Users
                      where n.id == credentials.id && n.Password == credentials.Password
                      select n.id).FirstOrDefault().ToString();

        if (!string.IsNullOrEmpty(userId))
        {
            //Following mehtod generates a token and stores it in db for further usage
            tokenInfo = tokenFunctions.GenerateToken(userId);
            if (tokenInfo != null)
            {
                //Creating response message with custom headers info
                response = Request.CreateResponse(HttpStatusCode.OK, userId);
                response.Headers.Add("Token", tokenInfo.AuthToken);
                response.Headers.Add("TokenExpiry", ConfigurationManager.AppSettings["AuthTokenExpiry"]);
                response.Headers.Add("Access-Control-Expose-Headers", "Token");
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            result = ResponseMessage(response);
            return result;
        }

        return Unauthorized();
    }

When I am accessing the above method through fiddler, I am able to see Token and TokenExpiry headers under "Miscellaneous" section. But when I am trying to access this method from angular JS application, I am not able to see Token/TokenExpiry headers in headers parameter of $http.post() call back method. Could anyone please help me to resolve this issue? Your reply helps me a lot.

3
  • where is the token stored? in local storage of browser? What i have done before was after i get the response from API i saved it in local storage using LocalStorageModule service so everytime I request access to api server I implemented an interceptor to retrieve the token from the local storage and append the token to the header. Commented Mar 8, 2016 at 6:57
  • I am new to Angular JS and I just wanted to print the token into console using $log.info. Currently I am not using localStorage service and interceptor. Will implement this solution and check. Thank you. Commented Mar 8, 2016 at 7:20
  • jsfiddle.net/9as2ac3d this is my login implementation nevermind for the html part Commented Mar 8, 2016 at 7:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.