Esempio n. 1
0
        /// <summary>
        /// Get the user credentials
        /// </summary>
        /// <param name="section">The config section group and section name.</param>
        /// <returns>The user credentials.</returns>
        /// <exception cref="System.Exception">Configuration load exception is thrown.</exception>
        public Nequeo.Security.UserCredentials GetUserCredentials(string section = "NequeoSecurityGroup/NequeoSecurityCredentials")
        {
            Nequeo.Security.UserCredentials credentials = null;

            try
            {
                // Refreshes the named section so the next time that it is retrieved it will be re-read from disk.
                System.Configuration.ConfigurationManager.RefreshSection(section);

                // Create a new default host type
                // an load the values from the configuration
                // file into the default host type.
                SecurityCredentials defaultHost =
                    (SecurityCredentials)System.Configuration.ConfigurationManager.GetSection(section);

                // Make sure the section is defined.
                if (defaultHost == null)
                {
                    throw new Exception("Configuration section has not been defined.");
                }

                // Get the user credetials element.
                UserCredentialsElement userCredentials = defaultHost.UserCredentialsSection;
                if (userCredentials == null)
                {
                    throw new Exception("Configuration element UserCredentials has not been defined.");
                }

                // Add the credentials.
                credentials = new UserCredentials()
                {
                    Username           = userCredentials.Username,
                    Password           = userCredentials.Password,
                    Domain             = userCredentials.Domain,
                    AuthenticationType = userCredentials.AuthenticationType,
                    AuthorisationType  = userCredentials.AuthorisationType,
                    ApplicationName    = userCredentials.ApplicationName,
                };

                // Get the authorisation credetials element.
                AuthorisationCredentialsElement authorisationCredentials = userCredentials.AuthorisationCredentials;
                if (authorisationCredentials != null)
                {
                    // Add the credentials.
                    Nequeo.Security.AuthorisationCredentials authCredentials = new AuthorisationCredentials()
                    {
                        Username         = authorisationCredentials.Username,
                        Password         = authorisationCredentials.Password,
                        Server           = authorisationCredentials.Server,
                        SecureConnection = authorisationCredentials.SecureConnection,
                        ContainerDN      = authorisationCredentials.ContainerDN
                    };

                    // Add the credentials
                    credentials.AuthorisationCredentials = authCredentials;
                }
            }
            catch (Exception)
            {
                throw;
            }

            // Return the credentials.
            return(credentials);
        }
Esempio n. 2
0
        /// <summary>
        /// Validate the current client.
        /// </summary>
        /// <param name="user">The current user principal.</param>
        /// <param name="authenticationSchemes">The authentication type.</param>
        /// <returns>True if the client has been validated; else false.</returns>
        public override bool ClientValidation(System.Security.Principal.IPrincipal user, Nequeo.Security.AuthenticationType authenticationSchemes)
        {
            // Does the user priciple exist.
            if (user != null)
            {
                // Does the user identity exist.
                if (user.Identity != null)
                {
                    // Select the curent Authentication Schemes
                    switch (authenticationSchemes)
                    {
                    case Nequeo.Security.AuthenticationType.User:
                    case Nequeo.Security.AuthenticationType.Basic:
                        // If the authentication type is 'Basic'

                        // If the identity is IIdentityMember.
                        if (user.Identity is Nequeo.Security.IdentityMember)
                        {
                            // The username and password are passed for
                            // Basic authentication type.
                            Nequeo.Security.IdentityMember identityMember = (Nequeo.Security.IdentityMember)user.Identity;

                            // Return the result of the authentication.
                            return(_provider.AuthenticateUser(identityMember.GetCredentials()));
                        }

                        // If the identity is HttpListenerBasicIdentity.
                        if (user.Identity is HttpListenerBasicIdentity)
                        {
                            // The username and password are passed for
                            // Basic authentication type.
                            HttpListenerBasicIdentity httpListenerBasicIdentity = (HttpListenerBasicIdentity)user.Identity;
                            string userName = httpListenerBasicIdentity.Name;
                            string password = httpListenerBasicIdentity.Password;

                            // Create the user credentials.
                            Nequeo.Security.UserCredentials credentials = new Nequeo.Security.UserCredentials();
                            credentials.Username = userName;
                            credentials.Password = password;

                            // Return the result of the authentication.
                            return(_provider.AuthenticateUser(credentials));
                        }
                        return(false);

                    case Nequeo.Security.AuthenticationType.Integrated:
                        // If the authentication type is WindowsIdentity
                        if (user.Identity is System.Security.Principal.WindowsIdentity)
                        {
                            WindowsIdentity windowsIdentity = (WindowsIdentity)user.Identity;
                            if (user.IsInRole("Administrators") || user.IsInRole("Users"))
                            {
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        break;

                    case Nequeo.Security.AuthenticationType.None:
                    case Nequeo.Security.AuthenticationType.Anonymous:
                        return(true);

                    default:
                        return(false);
                    }
                }
            }
            return(false);
        }