다음을 통해 공유


Developer’s Tools: How To Generate Basic Authentication Token

This demo is another tool worked out during an assignment while working with an integration scenario using web services supporting Basic Authentication.

Problem with basic authentication is that you have to keep username and password stored somewhere in order to generate the authentication token.

Since this was integrating external web services with SharePoint so we felt to delegate the Token Generation Process to an external tool and consume the Authentication Token directly without keeping username and password to be stored in the code itself.

To run this demo we have created a simple interface that takes username and password as input and generates the Basic Authentication Token and will display it in “Authentication Token” section

https://howtodowithsharepoint.files.wordpress.com/2017/03/11.png?w=800

Now let’s discuss the code for this functionality:

Step 1 is registering button click event by mapping a function** “get Token”**

**Step 2 is calling “getBasicAuthenticationToken” **function by passing username and password

Step 3 is preparing format required to convert plain text into hash value in base-64 format

Step 4 is calling JavaScript function “btoa” which will encode the plain text into “base-64” format which is a required hash value to prepare Basic Authentication Token.

“Basic” is prefixed to the hash value to comply with Basic Authentication Token Standards

https://howtodowithsharepoint.files.wordpress.com/2017/03/21.png?w=800

That is it for the code.

Now when we click “Get Authentication Token” button we will see the authentication token in the “Authentication Token” section.

https://howtodowithsharepoint.files.wordpress.com/2017/03/31.png?w=800

This token can be used for any Web Service supporting basic authentication, and this strategy can be merged with other functionalities too in order to generate this token on the fly.
Hope you find it helpful.