Wednesday, February 22, 2012

set and remove impersonation programmatically / runtime

/// <summary>
    /// <Description> This code is from http://support.microsoft.com/kb/306158 </Description>
    /// </summary>
    public class CallImpersonation
    {
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;
        WindowsImpersonationContext impersonationContext;
        [DllImport("advapi32.dll")]
        public static extern int LogonUserA(String lpszUserName, String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel,
            ref IntPtr hNewToken);
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);
        /// <summary>
        /// Turn on the impersonation
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="domain"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ImpersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity ident = WindowsIdentity.GetCurrent();
            WindowsPrincipal user = new WindowsPrincipal(ident);
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;
            if (RevertToSelf())
            {
                if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if (impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }
            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
        }
        /// <summary>
        /// Trunoff the impersonation.
        /// </summary>
        public void UndoImpersonation()
        {
            impersonationContext.Undo();
        }
        /// <summary>
        /// <Description>Get the credential for impersonation</Description>
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="domain"></param>
        /// <param name="password"></param>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="eBase"></param>
        /// <returns></returns>
        public bool GetImpersonateCredentials(out string userName, out string domain, out string password, string client, int key, ErgoBase eBase)
        {
            ErgoBase ebase = eBase;
            try
            {
                string fetchForExistingSql = "SELECT egemimp_guid,domainName,username,password_ue FROM egem_impersonation " +
                  "WHERE client = @client " +
                  "AND valid=   @valid " +
                  "AND status = @status ";
                ebase.Db.AddParameter("@client", client);
                ebase.Db.AddParameter("@valid", 1);
                ebase.Db.AddParameter("@status", "N");
                System.Data.DataTable dtforRegisteredusers = ebase.Db.GetDataTable(fetchForExistingSql);
                if (dtforRegisteredusers.Rows.Count > 0)
                {
                    domain = dtforRegisteredusers.Rows[0]["domainName"].ToString();
                    userName = dtforRegisteredusers.Rows[0]["username"].ToString();
                    password = EmploymentSettings.Decrypt(dtforRegisteredusers.Rows[0]["password_ue"].ToString(), key);
                    return true;
                }
                else
                {
                    domain = string.Empty;
                    userName = string.Empty;
                    password = string.Empty;
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
   

Tuesday, February 21, 2012

BitFlag and checkboxes

  <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CheckBox ID="CheckBox2" runat="server" />
        <asp:CheckBox ID="CheckBox3" runat="server" />
        <asp:CheckBox ID="CheckBox4" runat="server" />
 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

In code behind:

 public enum BitFlag : byte
        {
            First = 1,
            Second = 2,
            Third = 4,
            Fourth = 8
        }
        public  void CheckFlag(BitFlag bFlag)
        {
            if ((bFlag & BitFlag.First) == BitFlag.First)
                CheckBox1.Checked = true;
            if ((bFlag & BitFlag.Second) == BitFlag.Second)
                CheckBox2.Checked = true;
            if ((bFlag & BitFlag.Third) == BitFlag.Third)
                CheckBox3.Checked = true;
            if ((bFlag & BitFlag.Fourth) == BitFlag.Fourth)
                CheckBox4.Checked = true;
          
        }

   protected void Button1_Click(object sender, EventArgs e)
        {
            BitFlag bFlag = new BitFlag();//= BitFlag.First;
            if (CheckBox1.Checked)
                bFlag |= BitFlag.First;
            if (CheckBox2.Checked)
                bFlag |= BitFlag.Second;
            if (CheckBox3.Checked)
                bFlag |= BitFlag.Third;
            if (CheckBox4.Checked)
                bFlag |= BitFlag.Fourth;
              CheckFlag(bFlag);

        }

Thanks
Mahesh

Monday, February 20, 2012

Set Div Scroll bar postion by code.

Hi in follwoing example we set the div scrollbar postion by passing predefined value. we can calculate this value depend on our requirement.
How to retrive current scroll top of div?
Add follwoing event in div
onscroll="MaintanDivScrollPosition(this)"
Implement function and store scrolltop in hiddenfield.
 
function MaintanDivScrollPosition(sender) {"input[type=hidden][id$=HiddenFieldScrollTopDivContentPage]").val($(sender)[0].scrollTop);

$(
}

<html>
    <head>
        <title>OnScroll Example</title>
        <script type="text/javascript">
            function CallOnLoad () {
               document.getElementById("divrelative").scrollTop = 500;
             
            }
        </script>
    </head>
    <body onload="CallOnLoad()">
        <div id="divrelative" style="overflow:scroll;height:200px;">
         <P>Line 1</p>
         <P>Line 2</p>
         <P>Line 3</p>
         <P>Line 4</p>
         <P>Line 5</p>
         <P>Line 6</p>
         <P>Line 7</p>
         <P>Line 8</p>
         <P>Line 9</p>
         <P>Line 10</p>
         <P>Line 11</p>
         <P>Line 12</p>
   <P>Line 14</p>
         <P>Line 15</p>
         <P>Line 16</p>
         <P>Line 17</p>
         <P>Line 18</p>
         <P>Line 19</p>
         <P>Line 20</p>
         <P>Line 21</p>
         <P>Line 22</p>
</div>
    </body>
</html>