Quantcast
Channel: Azure Active Directory forum
Viewing all articles
Browse latest Browse all 16000

Log in to Graph API with User Credentials instead of SPN

$
0
0

Basically, instead of creating a spn with keys et al I want to log in with "my" (user) credentials (ie admin@something.onmicrosoft.com).

Is that possible? If so, how do I rewrite (old version):

        private static string GetAuthorizationHeader()
        {
            string authzHeader = null;
            AuthenticationContext _authContext = new AuthenticationContext(fullTenantName);

            try
            {
                SymmetricKeyCredential credential = new SymmetricKeyCredential(issuingResource, Convert.FromBase64String(servicePrincipalSymmetricKey));
                AssertionCredential _assertionCredential = _authContext.AcquireToken(serviceRealm, credential);
                authzHeader = _assertionCredential.CreateAuthorizationHeader();
            }
            catch (AALException aalEx)
            {
                Debug.WriteLine(aalEx.ToString());
            }

            return authzHeader;
        }

or (the new version):

        public static string GetAuthorizationToken(string tenantName, string appPrincipalId, string password)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://accounts.accesscontrol.windows.net/tokens/OAuth/2");
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            string postData = "grant_type=client_credentials";
            string graphPrincipalId = "00000002-0000-0000-c000-000000000000";
            postData += "&resource=" + HttpUtility.UrlEncode(graphPrincipalId + "/" + "graph.windows.net" + "@" + tenantName);
            postData += "&client_id=" + HttpUtility.UrlEncode(appPrincipalId + "@" + tenantName);
            postData += "&client_secret=" + HttpUtility.UrlEncode(password);
            byte[] data = encoding.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            using (var response = request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AcsTokenFormat));
                    AcsTokenFormat token = (AcsTokenFormat)(ser.ReadObject(stream));
                    return String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", token.token_type, " ", token.access_token);
                }
            }
        }

Thanks,

 Chris


Christoph Wille - Glengamoi Alumni - Realnamen sind ein Gebot der Höflichkeit in der Community


Viewing all articles
Browse latest Browse all 16000

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>