User Authentication in ASP.NET Web API

前端 未结 3 589
醉话见心
醉话见心 2020-12-02 03:52

This topic has been incredibly confusing for me. I am a rookie in HTTP apps but need to develop an iPhone client that consumes JSON data from somewhere. I chose Web API from

3条回答
  •  清歌不尽
    2020-12-02 04:05

    I am working on a MVC5/Web API project and needed to be able to get authorization for the Web Api methods. When my index view is first loaded I make a call to the 'token' Web API method which I believe is created automatically.

    The client side code (CoffeeScript) to get the token is:

    getAuthenticationToken = (username, password) ->
        dataToSend = "username=" + username + "&password=" + password
        dataToSend += "&grant_type=password"
        $.post("/token", dataToSend).success saveAccessToken
    

    If successful the following is called, which saves the authentication token locally:

    saveAccessToken = (response) ->
        window.authenticationToken = response.access_token
    

    Then if I need to make an Ajax call to a Web API method that has the [Authorize] tag I simply add the following header to my Ajax call:

    { "Authorization": "Bearer " + window.authenticationToken }
    

提交回复
热议问题