Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientInfo"/> class.
        /// </summary>
        /// <param name="parent">An <see cref="ClientHelper"/> object.</param>
        public ClientInfo(ClientHelper parent)
        {
            // Initialize member variables.
            m_clientType = Common.GetApplicationType();
            m_machineName = Environment.MachineName;

            // Initialize user principal.
            if (m_clientType == ApplicationType.Web)
                m_clientUser = new GenericPrincipal(new GenericIdentity(UserInfo.RemoteUserID), new string[] { });
            else
                m_clientUser = new GenericPrincipal(new GenericIdentity(UserInfo.CurrentUserID), new string[] { });

            // Initialize user credentials.
            if (parent == null || string.IsNullOrEmpty(parent.Username) || string.IsNullOrEmpty(parent.Password))
                m_clientUserCredentials = string.Empty;
            else
                m_clientUserCredentials = string.Format("{0}:{1}", parent.Username, parent.Password);

            // Initialize client application name.
            if (m_clientType == ApplicationType.Web)
            {
                if (HostingEnvironment.ApplicationVirtualPath == "/")
                    m_clientName = HostingEnvironment.SiteName;
                else
                    m_clientName = HostingEnvironment.ApplicationVirtualPath.Trim('/');
            }
            else
            {
                m_clientName = AssemblyInfo.EntryAssembly.Name;
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientInfo"/> class.
        /// </summary>
        /// <param name="parent">An <see cref="ClientHelper"/> object.</param>
        public ClientInfo(ClientHelper parent)
        {
            m_clientID = Guid.Empty;
            m_clientType = Common.GetApplicationType();
            m_machineName = Environment.MachineName;

            // Get the user login id.
            if (!string.IsNullOrEmpty(UserInfo.RemoteUserID))
                m_userName = UserInfo.RemoteUserID;
            else
                m_userName = UserInfo.CurrentUserID;

            // Get the type of client application.
            if (ClientType == ApplicationType.WindowsCui || ClientType == ApplicationType.WindowsGui)
                m_clientName = AppDomain.CurrentDomain.FriendlyName;
            else if (ClientType == ApplicationType.Web)
                m_clientName = HttpContext.Current.Request.ApplicationPath;

            // Initialize the serialized identity token.
            m_serializedIdentityToken = string.Empty;
            if (parent != null && parent.AuthenticationMethod != IdentityToken.None)
            {
                SecurityToken token = null;
                StringWriter stringWriter = new StringWriter();
                XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
                SerializableTokenWrapper<SecurityToken> serializer = new SerializableTokenWrapper<SecurityToken>();

                try
                {
                    // Create a token based on the selected method.
                    if (parent.AuthenticationMethod == IdentityToken.Ntlm)
                    {
                        if (!string.IsNullOrEmpty(parent.AuthenticationInput) && 
                            parent.AuthenticationInput.Contains(":"))
                        {
                            // Input format: <username>:<password>
                            string[] loginParts = parent.AuthenticationInput.Split(':');
                            token = new UsernameToken(loginParts[0], loginParts[1], PasswordOption.SendPlainText);
                        }
                    }
                    else if (parent.AuthenticationMethod == IdentityToken.Kerberos)
                    {
                        if (!string.IsNullOrEmpty(parent.AuthenticationInput) &&
                            parent.AuthenticationInput.Contains("/"))
                        {
                            // Input format: host/<machine name>
                            token = new KerberosToken(parent.AuthenticationInput, ImpersonationLevel.Impersonation);
                        }
                    }

                    // Serialize the token to XML for transportation.
                    if (token != null)
                    {
                        serializer.WriteToken(xmlTextWriter, token);
                        m_serializedIdentityToken = stringWriter.ToString();
                    }
                }
                catch
                {
                    // Identity token creation failed due to an exception.
                }
            }
        }