コード例 #1
0
        /// <inheritdoc />
        public async Task <TokenDTO> MicrosoftLogin(MicrosoftLoginModel model)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, $"{_microsoftConfiguration.GraphUrl}/oidc/userinfo");

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", model.AuthToken);
            var client = _httpClientFactory.CreateClient();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");

            var response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                Dictionary <string, object> values = JsonConvert.DeserializeObject <Dictionary <string, object> >(result);

                string subject    = (string)values["sub"];
                string email      = (string)values["email"];
                string givenName  = (string)values["given_name"];
                string familyName = (string)values["family_name"];

                return(await CreateExternalUser(model.Provider, subject, email, $"{givenName} {familyName}"));
            }

            throw new ArgumentException("Cannot connect to the Microsoft");
        }
コード例 #2
0
        public async Task <TokenDTO> MicrosoftLogin([FromBody] MicrosoftLoginModel model)
        {
            if (_applicationConfiguration.DisableRegistration || _msConfiguration.LoginDisabled)
            {
                throw new Exception("External login is disabled");
            }

            return(await _authService.MicrosoftLogin(model));
        }