0

I have create a html file and hosted it in azure app service(kudos-wwwroot folder) and enabled ad authentication by enabling app registration

Is there any way to capture ad logged in user mail id in html file?

The extension are used in html page are js and css.

  • Could you pls describe what is 'log in user mail id'? Can I understand it as the account which used to sign in your app? – tiny-wa Mar 04 '21 at 02:04
  • HI @Tiny-wa i meant that when we login using ad creds i need to capture user detalis like user name and user mail in that specific html page – Krishnareddy Chinthireddy Mar 04 '21 at 03:45
  • If you have any further problem, pls feel free to let us know, your problems now may be others' in the future, thanks. – tiny-wa Mar 05 '21 at 02:02

1 Answers1

0

Thanks for response, and I think this may help you. If you enable the authentication following this tutorial, I think you can call /.auth/me to obtain the userName claim, here's another answer to this situation, I also tried it, it must help.

And if you use msal.js for achieving the authentication, it's also easy to gather user information by call graph api(https://graph.microsoft.com/v1.0/me), and here's a sample.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="js/msal.js"></script>
    </head>
    <body>
            <div style="margin-top: 15px; background-color: #DDDDDD;">
                <button type="button" id="signIn" onclick="signIn()">Sign In</button>
                <button type="button" id="getAccessToken" onclick="getAzureAccessToken()">getAccessToken</button>
                <button type="button" id="accessApi" onclick="accessApi()">getApiResponse</button>
                <h5 class="card-title" id="welcomeMessage">Please sign-in to see your profile and read your mails</h5>
                <div>
                    <div>
                        accesstoken :
                        <div id="accesstoken">
                            
                        </div>
                    </div>
                    <div id="">
                        api response :
                        <div id="json">
                            
                        </div>
                    </div>
                </div>
            </div>
            <script type="text/javascript">
                const msalConfig = {
                    auth: {
                        clientId: "application_id",
                        authority: "https://login.microsoftonline.com/your_tenant",
                        redirectUri: "http://localhost:8848/implicitGrantFlowAuthAndCallApi/new_file.html",
                    },
                    cache: {
                        cacheLocation: "sessionStorage", // This configures where your cache will be stored
                        storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
                    }
                };
        
                const loginRequest = {
                    scopes: ["openid", "profile", "User.Read"]
                };
                //scope for generate access token
                const AzureMgmtScops ={
                    scopes:["User.Read"]
                }
                //api endpoint
                const apiConf = {
                    endpoint:"https://graph.microsoft.com/v1.0/me"
                };
                
                let accessToken = '';
                const myMSALObj = new Msal.UserAgentApplication(msalConfig);
        
                function signIn() {
                    myMSALObj.loginPopup(loginRequest)
                        .then(loginResponse => {
                            console.log("id_token acquired at: " + new Date().toString());
                            console.log(loginResponse);
        
                            if (myMSALObj.getAccount()) {
                                showWelcomeMessage(myMSALObj.getAccount());
                                getAzureAccessToken();
                            }
                        }).catch(error => {
                            console.log(error);
                        });
                }
        
                function showWelcomeMessage(account) {
                    document.getElementById("welcomeMessage").innerHTML = `Welcome ${account.name}`;
                }
        
                function getAzureAccessToken(){
                    myMSALObj.acquireTokenSilent(AzureMgmtScops).then(tokenResponse => {
                        showAccesstoken(tokenResponse.accessToken)
                        accessToken = tokenResponse.accessToken;
                        sessionStorage.setItem("accessToken", accessToken);
                        callMSGraph(apiConf.endpoint, tokenResponse.accessToken, showResult);
                    }).catch(function (error) {
                         console.log(error);
                    })
                }
                
                function accessApi(){
                    callMSGraph(apiConf.endpoint, accessToken, showResult);
                }
        
                function callMSGraph(endpoint, token, callback) {
                    const headers = new Headers();
                    const bearer = `Bearer ${token}`;
        
                    // headers.append("Content-Type", "application/json");
                    headers.append("Authorization", bearer);
        
                    const options = {
                        method: "GET",
                        headers: headers
                    };
        
                    console.log('request made to Graph API at: ' + new Date().toString());
        
                    fetch(endpoint, options)
                        .then(response => response.json())
                        .then(response => callback(response, endpoint))
                        .catch(error => console.log(error))
                }
                
                function showAccesstoken(data){
                    document.getElementById("accesstoken").innerHTML = JSON.stringify(data, null, 2);
                }
                
                function showResult(data){
                    document.getElementById("json").innerHTML = JSON.stringify(data, null, 2);
                }
            </script>
    </body>
</html>
tiny-wa
  • 1,551
  • 1
  • 3
  • 10