Click here to Skip to main content
15,942,545 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to implement custom token based authentication and authorization in angular JS and ASP.NET Web Api.

I have the following log in method, which will check for user details in database and if user exists it generates a token and adds it into the response headers. But I am not able to access the Token header in angular JS call back method. Can anybody let me know how to access this token from angular application?

What I have tried:

        public IHttpActionResult Authenticate([FromBody]LoginModel credentials)
            IHttpActionResult result;
            HttpResponseMessage response;
            TokenModel tokenInfo;
            var userId = (from n in _data.Users
                          where == && n.Password == credentials.Password

            if (!string.IsNullOrEmpty(userId))
                tokenInfo = tokenFunctions.GenerateToken(userId);
                if (tokenInfo != null)
                    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");
                    response = Request.CreateResponse(HttpStatusCode.Unauthorized);

                result = ResponseMessage(response);
                return result;

            return Unauthorized();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900