/// <summary> /// Questo metodo base relativo al flusso Password Grant non verrà mai chiamato /// </summary> /// <param name="authenticationUserRequest"></param> /// <returns></returns> public override IAuthenticationUserResponseDto LogonUser(AuthenticationUserRequestDto authenticationUserRequest) { return(new AuthenticationUserResponseDto { FailCode = "NotImplemented", FailMessage = "This is a ImplicitFlow logon Provider", LogonSucceeded = false }); }
public override IAuthenticationUserResponseDto LogonUser(AuthenticationUserRequestDto authenticationUserRequest) { IAuthenticationUserResponseDto response = new AuthenticationUserResponseDto(); try { if (authenticationUserRequest == null) { throw new ArgumentNullException(nameof(authenticationUserRequest)); } if (string.IsNullOrWhiteSpace(authenticationUserRequest.Username)) { throw new ArgumentNullException(nameof(authenticationUserRequest.Username)); } if (string.IsNullOrWhiteSpace(authenticationUserRequest.Password)) { throw new ArgumentNullException(nameof(authenticationUserRequest.Password)); } // Validazione parametri Logger.Debug($"Logon Provider {_description} -> LogonUser: {authenticationUserRequest.Username} Password: {authenticationUserRequest.Password?.Any()} ClientId: {authenticationUserRequest.ClientId} ClientId: {authenticationUserRequest.ClientId} ClientVersion: {authenticationUserRequest.ClientVersion} MachineKey: {authenticationUserRequest.MachineKey} ClientIpAddress: {authenticationUserRequest.ClientIpAddress}"); // Recupero parametri logon provider var logonProviderConfiguration = GetProviderConfiguration(); var tokenUrl = logonProviderConfiguration.FirstOrDefault(x => x.Name.Equals(IdentityServerTokenUrlParam))?.Value as string; if (string.IsNullOrWhiteSpace(tokenUrl)) { throw new Exception(string.Format("Wrong logon provider configuration parameter: {0}", IdentityServerTokenUrlParam)); } var clientId = logonProviderConfiguration.FirstOrDefault(x => x.Name.Equals(ClientIdParam))?.Value as string; if (string.IsNullOrWhiteSpace(clientId)) { throw new Exception(string.Format("Wrong logon provider configuration parameter: {0}", ClientIdParam)); } var secret = logonProviderConfiguration.FirstOrDefault(x => x.Name.Equals(ClientSecretParam))?.Value as string; if (string.IsNullOrWhiteSpace(secret)) { throw new Exception(string.Format("Wrong logon provider configuration parameter: {0}", ClientSecretParam)); } // https://identitymodel.readthedocs.io/en/latest/client/token.html // http://www.bubblecode.net/en/2016/01/22/understanding-oauth2/#Resource_Owner_Password_Credentials_Grant using (var client = new HttpClient()) { Task.Run(() => { var authenticationResultTask = client.RequestPasswordTokenAsync(new PasswordTokenRequest { Address = tokenUrl, ClientSecret = secret, ClientId = clientId, Scope = "openid profile email", // basterebbe openid, aggiungo anche le info del profilo per poter capire l'identità di chi si sta loggando UserName = authenticationUserRequest.Username, Password = authenticationUserRequest.Password, Parameters = new Dictionary <string, string> { // Posso passare dei parametri ulteriori a Identity Server { "ClientVersion", authenticationUserRequest.ClientVersion }, { "ClientIpAddress", authenticationUserRequest.ClientIpAddress }, { "LanguageCultureName", authenticationUserRequest.LanguageCultureName }, } }); return(authenticationResultTask); }).ContinueWith(task => { var authenticationResult = task.Result; if (authenticationResult.IsError) { // Loggo tutto l'oggetto Logger.Error(JsonConvert.SerializeObject(authenticationResult)); response.LogonSucceeded = false; response.FailCode = "AuthenticationFailed"; response.FailMessage = "Wrong username or password"; } else { response.LogonSucceeded = true; } }, TaskContinuationOptions.OnlyOnRanToCompletion) .Wait(); } } // Eccezione sollevata dal Task catch (AggregateException aggregatedException) { Logger.Error($"{MethodBase.GetCurrentMethod().Name} Logon provider {_description}", aggregatedException.InnerException); response.LogonSucceeded = false; response.FailCode = "AuthenticationFailed"; response.FailMessage = aggregatedException.InnerException?.ToString(); } catch (Exception e) { Logger.Error($"{MethodBase.GetCurrentMethod().Name} Logon provider {_description}", e); response.LogonSucceeded = false; response.FailCode = "AuthenticationFailed"; response.FailMessage = e.Message; } return(response); }