2

I am trying to learn Angular2

and I am trying to create a simple blog with authentication.

this here is my add a new post method:

    [Authorize]
    // POST: api/Post
    public PostModel Post([FromBody]PostViewModel model)
    {             
        var post = new PostModel
        {
            Body = model.Body,
            Title = model.Title,
            AuthorId = IdentityExtensions.GetUserId(User.Identity),
        };
        var res = blogRepo.AddPost(post);
        return res;                 
    }

everything works fine, but IdentityExtension.GetUserId() do not return the most current logged in user but the first user since the app started.

basically I am looking for a way to make sure that the current user logs out on the server as well as on the client (the client side is just a simple removal of the localStorage.removeItem("jwt");)

also there is a good chance that what I am doing is totally wrong, but I can't access the ApplicationUserManager in this controller.

4
  • How do you send the authentication hints within your Angular2 application? Thanks! Commented Apr 10, 2016 at 16:41
  • I have made two Headers in my angular app, one is for normal calls, and the other is for authenticated calls such as this one, basically have stored an access-token in localStorage when the user logs in, and send this token in the header using authorazation: Bearer + token, on the client side the tokens changes when the user logs out and a new user logs in but not the server Commented Apr 10, 2016 at 16:46
  • Is IdentityExtensions.GetUserId(User.Identity) using some sort of caching? It seems strange that it always returns the first user logged in. How does it use the User.Identity parameter? Commented Apr 11, 2016 at 2:24
  • I am really not sure if it uses caching (I am totally a newbie on both angular and web api + token based authentication), I think the trouble here is that token based auth is different from normal stored in data base logins, I just have to wrap my head around the concept Commented Apr 11, 2016 at 7:31

1 Answer 1

1

ok I have found the problem, although I haven't managed to solve it yet but I will update this when i do, and I am writing this as an answer since the problem is totally different from what I asked and thought to be.

the problem is related to sending the authentication hints as Thierry Templier suggested. I have a file that exports headers like this:

  export const authHeaders = new Headers();
    authHeaders.append('Accept', 'application/json');
    authHeaders.append('Content-Type', 'application/json');
    authHeaders.append('Authorization', 'Bearer ' + localStorage.getItem('jwt'));

And I Import this header where ever I need it. but I am not sure why it always sends a cached value (i.e the first value stored on the client and it has nothing to do with the server side as my question implies).

to solve this issue I just have to make sure the latest access-token stored on localstorage is sent to the server.

EDIT: for now I am constructing the headings in the constructor.

Sign up to request clarification or add additional context in comments.

Comments

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.