示例#1
0
        public ActionResult Index()
        {
            string tenantId = CustomAuthenticationManager.GetTenantID();
            string userId   = CustomAuthenticationManager.GetUserID();

            string urlRestTemplate = "https://graph.windows.net/{0}/users/{1}?api-version=1.5";
            string urlRest         = string.Format(urlRestTemplate, tenantId, userId);
            Uri    uriRest         = new Uri(urlRest);

            string accessToken = CustomAuthenticationManager.GetAccessToken();

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
            client.DefaultRequestHeaders.Add("Accept", "application/json");

            HttpResponseMessage response = client.GetAsync(uriRest).Result;

            if (response.IsSuccessStatusCode)
            {
                string json     = response.Content.ReadAsStringAsync().Result;
                ADUser userInfo = JObject.Parse(json).ToObject <ADUser>();
                return(View(userInfo));
            }
            else
            {
                throw new ApplicationException("Whoops");
            }
        }
        public ActionResult AppOnlyAccessToken()
        {
            string urlTokenEndpointForTenant =
                string.Format("https://login.windows.net/{0}/oauth2/token",
                              CustomAuthenticationManager.GetTenantID());

            ADAL.AuthenticationContext authenticationContext
                = new ADAL.AuthenticationContext(urlTokenEndpointForTenant);

            string resource = DemoConstants.TargetResource;

            ADAL.ClientAssertionCertificate clientAssertionCertificate =
                DemoConstants.GetClientAssertionCertificate();

            ADAL.AuthenticationResult authenticationResult =
                authenticationContext.AcquireToken(resource, clientAssertionCertificate);

            string           appOnlyAccessToken    = authenticationResult.AccessToken;
            JwtSecurityToken jwtAppOnlyAccessToken = new JwtSecurityToken(appOnlyAccessToken);

            return(View(jwtAppOnlyAccessToken));
        }