Jwalin Khatri

Encrypting the Web.Config File

Posted by: jwalin on: June 26, 2009

Thank you Peter Vogel

Encrypting Sections
This is the code that I use to encrypt a specific section of the Web.config file. I first select a section using the Configuration object’s Sections collection. In this case, I’m selecting the connectionStrings section:

Dim configFile As System.Configuration.Configuration
Dim configSection As ConfigurationSection

configFile = System.Web.Configuration.WebConfigurationManager. _
OpenWebConfiguration(Request.ApplicationPath)
configSection = configFile.Sections("connectionStrings")

This code assumes that it’s running from a page on the site so I can use the ApplicationPath property on the Request object to get the physical path to the web.config’s folder. If you wanted to create a utility, you’d need to hard code the path you pass to the OpenWebConfiguration method.

Now that I have the section, I encrypt it, specifying the encryption scheme that I want to use. The last step is to save the encrypted version back to the config file:

configSection.SectionInformation. _
ProtectSection("RsaProtectedConfigurationProvider")
configFile.Save()

The result looks like this in the Web.config file:

<connectionStrings
configProtectionProvider=”DataProtectionConfigurationProvider”>
<EncryptedData>
<CipherData>
<CipherValue>…encrypted data… </CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>

The good news is that when you use the ConfigurationManager’s ConnectionString collection to retrieve an encrypted connection string, the connection string is automatically decrypted for you. This code works whether the connectionStrings section is encrypted or not:

Dim cnStr As String
cnStr = System.Web.Configuration.WebConfigurationManager. _
ConnectionStrings("Northwind").ConnectionString

Since the strings are encrypted using the private key for the Web server, even if the file is stolen from the site, it can’t be decrypted on any machine except the Web server. This also means that you can’t encrypt the connection string until the application has been moved to the production server: If you encrypt the connection string on the test server and then move your site to the production server, ASP.NET won’t be able to decrypt the string using the production server’s private key.

On occasion, you’ll need to decrypt the web.config section just to check what’s actually in the file. This code takes care of that job:

Dim configFile As System.Configuration.Configuration
Dim configSection As ConfigurationSection

configFile = System.Web.Configuration.WebConfigurationManager. _
OpenWebConfiguration(Request.ApplicationPath)
configSection = configFile.Sections("connectionStrings")
configSection.SectionInformation.UnProtectSection()
configFile.Save()

Command Line Encryption
If you’d prefer not to use code, you encrypt (or decrypt) sections of your web.config file using the aspnet_regiis utility. You must pass the utility the -pe parameter to specify the section to encrypt along with the path name to the config file’s folder, and you must also pass the -prov parameter to specify the encryption scheme:

aspnet_regiis.exe -pef section physical_directory -prov provider

This example encrypts the configurationStrings section for a config file in the c:\NorthwindCRM folder:

aspnet_regiis.exe -pef configurationStrings c:\NorthwindCRM
-prov "RsaProtectedConfigurationProvider"

You can also use aspnet_regiis utility to decrypt the section using the -pdf parameter instead of -pef.

Or you could just make sure that no one can steal text files from your Web server.

NOTE:

    When you change the configuration file while running, all Sessions would be reset… and data would be lose well..
    The comment about changing the web.config file causing your application to (effectively) restart is an excellent point–and one I should have mentioned. You really do want to encrypt your config file !!AS SOON AS YOU DEPLOY IT!! and not after some users have started working with the application

IIS Search Engine Optimization Toolkit

Posted by: jwalin on: June 4, 2009

http://www.linkedin.com/news?actionBar=&sik=1244087646866&aIdx=0&articleID=40544986

Example, but with no error checking:

using System;
using System.Reflection;

namespace Foo
{
class Test
{
static void Main()
{
Type type = Type.GetType(”Foo.MyClass”);
object instance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod(”MyMethod”);
method.Invoke(instance, null);
}
}

class MyClass
{
public void MyMethod()
{
Console.WriteLine(”In MyClass.MyMethod”);
}
}
}
Each step needs careful checking – you may not find the type, it may not have a parameterless constructor, you may not find the method, you may invoke it with the wrong argument types.

One thing to note: Type.GetType(string) needs the assembly-qualified name of the type unless it’s in the currently executing assembly or mscorlib.

Store Procedure Vs. SSIS Package

Posted by: jwalin on: May 25, 2009

I have one task to convert the Store procedure into SSIS package. In store procedure it is using Cursor. By googling I tried to find an example but could not find good example. Here is my store procedure

DECLARE Agent_SkillGroup CURSOR FAST_FORWARD READ_ONLY FOR
SELECT DateTime,SkillTargetID,PeripheralID,sum(HandledCallsTalkTimeToHalf),count(*)
FROM WCI_IP_ST_AGENT_SKILL_GROUP_HH
WHERE SkillGroupSkillTargetID NOT IN (7820,10023,10793,13948,16368)
AND DateTime>= CAST(Convert(varchar(10), getdate()-1, 101) as datetime)
AND DateTime 0
DROP TABLE #Agent_Temp
SELECT 0 SkillGroupSkillTargetID,0 SkillTargetID,100.000000 R2,1000.00000 R21
INTO #Agent_Temp

OPEN Agent_SkillGroup

FETCH NEXT FROM Agent_SkillGroup INTO @date,@IPCCAgentTargetId,@PeripheralId,@AgntTotalTalkTime,@noAgentSG

WHILE @@FETCH_STATUS -1
BEGIN
SELECT @AgntAvailTimeOnDefaultSk = AvailTimeToHalf FROM WCI_IP_ST_AGENT_SKILL_GROUP_HH
WHERE DateTime=@date and SkillTargetID=@IPCCAgentTargetId and PeripheralId=@PeripheralId
and SkillGroupSkillTargetID = (
Select Case @PeripheralId
When 5000 Then 7820
When 5006 Then 10023
When 5016 Then 13948
When 5024 Then 16368
End
)

Truncate table #Agent_Temp

IF @noAgentSG>1
BEGIN
IF @AgntAvailTimeOnDefaultSk 0 ---No Available Time
BEGIN
IF @AgntTotalTalkTime=0 Select @AgntTotalTalkTime=1

INSERT INTO #Agent_Temp (SkillGroupSkillTargetID,SkillTargetID,R21,R2)
SELECT SkillGroupSkillTargetID,SkillTargetID , AvailTimeToHalf*1.000/ @AgntAvailTimeOnDefaultSk,0
FROM WCI_IP_ST_AGENT_SKILL_GROUP_HH
WHERE SkillTargetId=@IPCCAgentTargetId
AND Datetime = @date AND PeripheralId=@PeripheralId
AND SkillGroupSkillTargetID not in (7820,10023,10793,13948,16368)

SELECT @sumR21 = sum(R21) from #Agent_Temp

IF @sumR21 = 0.00
SELECT @sumR21=count(*) FROM #Agent_Temp

UPDATE #Agent_Temp SET R2=R21/@sumR21

If @AgntTotalTalkTime1
UPDATE WCI_IP_ST_AGENT_SKILL_GROUP_HH SET
Ratio1=HandledCallsTalkTimeToHalf*1.000/@AgntTotalTalkTime,
Ratio2=R2,
wcAvailableTime = convert (int,@AgntAvailTimeOnDefaultSk*1.0000 * ((HandledCallsTalkTimeToHalf*1.000/@AgntTotalTalkTime)+R2)/2)
FROM #Agent_Temp
WHERE WCI_IP_ST_AGENT_SKILL_GROUP_HH.DateTime=@date and WCI_IP_ST_AGENT_SKILL_GROUP_HH.SkillTargetID=@IPCCAgentTargetId
AND WCI_IP_ST_AGENT_SKILL_GROUP_HH.SkillGroupSkillTargetID=#Agent_Temp.SkillGroupSkillTargetID
AND WCI_IP_ST_AGENT_SKILL_GROUP_HH.SkillGroupSkillTargetID not in (7820,10023,10793,13948,16368)
ELSE
UPDATE WCI_IP_ST_AGENT_SKILL_GROUP_HH set
Ratio1=1.000/@noAgentSG, Ratio2=R2, wcAvailableTime = convert (int,@AgntAvailTimeOnDefaultSk*1.0000 * ((1.000/@noAgentSG)+R2)/2)
FROM #Agent_Temp
WHERE WCI_IP_ST_AGENT_SKILL_GROUP_HH.DateTime=@date and WCI_IP_ST_AGENT_SKILL_GROUP_HH.SkillTargetID=@IPCCAgentTargetId
AND WCI_IP_ST_AGENT_SKILL_GROUP_HH.SkillGroupSkillTargetID=#Agent_Temp.SkillGroupSkillTargetID
AND WCI_IP_ST_AGENT_SKILL_GROUP_HH.SkillGroupSkillTargetID not in (7820,10023,10793,13948,16368)

SELECT @AfterSplitSum = sum(wcAvailableTime) FROM WCI_IP_ST_AGENT_SKILL_GROUP_HH
WHERE
DateTime=@date AND SkillTargetID=@IPCCAgentTargetId
AND SkillGroupSkillTargetID NOT IN (7820,10023,10793,13948,16368)

IF @AfterSplitSum@AgntAvailTimeOnDefaultSk
BEGIN
SELECT @ErrCorrSkillId = SkillGroupSkillTargetID FROM WCI_IP_ST_AGENT_SKILL_GROUP_HH
WHERE SkillTargetID=@IPCCAgentTargetId and DateTime=@date
AND SkillGroupSkillTargetID not in (7820,10023,10793,13948,16368)
ORDER BY Ratio1 DESC

UPDATE WCI_IP_ST_AGENT_SKILL_GROUP_HH SET
wcAvailableTime= wcAvailableTime +(@AgntAvailTimeOnDefaultSk-@AfterSplitSum)
WHERE DateTime=@date AND SkillTargetID=@IPCCAgentTargetId
AND SkillGroupSkillTargetID = @ErrCorrSkillId
END ------ End Rounding correction
End --end @AgntAvailTimeOnDefaultSk 0 true
End --end @noAgentSG>1 true
Else
UPDATE WCI_IP_ST_AGENT_SKILL_GROUP_HH SET
wcAvailableTime= @AgntAvailTimeOnDefaultSk
WHERE DateTime=@date AND SkillTargetID=@IPCCAgentTargetId
AND SkillGroupSkillTargetID NOT IN (7820,10023,10793,13948,16368)

fetch next from Agent_SkillGroup into @date,@IPCCAgentTargetId,@PeripheralId,@AgntTotalTalkTime,@noAgentSG
END

CLOSE Agent_SkillGroup
DEALLOCATE Agent_SkillGroup

Here is the SSIS Package Screenshot
pacakge screenshot
and here is the package which is in doc file. but when you download it Change extension to .ZIP File
Get Agent Data

ArrayList to Two diamentional Array

Posted by: jwalin on: April 22, 2009

This code is used when you pass the HashTable to Web service

Here is the code to convert HashTable into two diamentional array to HashTable and two diamentional array to String.

public object[][] ToJaggedArray(Hashtable ht)
{
object[][] oo = new object[ht.Count][];
int i = 0;
foreach (object key in ht.Keys)
{
oo[i] = new object[] { key, ht[key] };
i++;
}
return oo;
}
public Hashtable ToHashtable(object[][] oo)
{
Hashtable ht = new Hashtable(oo.Length);
foreach (object[] pair in oo)
{
object key = pair[0];
object value = pair[1];
ht[key] = value;
}
return ht;
}

public string JaggedArrayToString(object[][] sender)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (object[] pair in sender)
{
//object key = pair[0];
//object value = pair[1];
sb.Append(pair[0].ToString()).Append(”\t”).Append(pair[1].ToString()).Append(”\n”);
}
return sb.ToString();
}

Get Windows Server 2008 Standard Edition

Posted by: jwalin on: April 14, 2009

If you are student and you want to play with Visual Studio 2008/2005 professional version, Windows Server 2008 Standard Edition, SQL Server 2008, Windows Server 2003, Virtual PC 2005 go to
www.DreamSpark.com

Cheers !!! Microsoft

http://interviews.dotnetthread.com/2009/02/making-gridview-rows-or-individual.html

Function to Add Leading 0’s (Zeros)

Posted by: jwalin on: March 5, 2009

 Thankx to By Joshua A. WalkerTESTING: 
SELECT dbo.DBA_fnAddLeadingZeros(2457,6)
Result: ‘002457”
***************************************************************************/
CREATE
FUNCTION [dbo].[DBA_fnAddLeadingZeros](@Int INT, @TotalLength INT)
RETURNS VARCHAR(2000)
AS
BEGIN
     DECLARE @Return VARCHAR(2000)

/************************************************************************

        –IF @TotalLength > 100 SELECT @TotalLength = 100
      SELECT @Return = replicate(‘0′,@TotalLength) + CAST(@Int AS VARCHAR)
      RETURN RIGHT(@Return,@TotalLength)

 END

Happy Programming !!!!

 If you want to delete all the records from the all the tables in database you might consider Delete Or Trancate command as both delete all the records from the tables. However it depends on ‘Constraint’ and delete records from one table at a time. But here is the code which delete the records from all the tables in one single shot…

 

CREATE 
AS

 PROCEDURE sp_DeleteAllData
    EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
    EXEC sp_MSForEachTable ‘ALTER TABLE ? DISABLE TRIGGER ALL’
    EXEC sp_MSForEachTable ‘DELETE FROM ?’
    EXEC sp_MSForEachTable ‘ALTER TABLE ? CHECK CONSTRAINT ALL’
    EXEC sp_MSForEachTable ‘ALTER TABLE ? ENABLE TRIGGER ALL’
    EXEC sp_MSFOREACHTABLE ‘SELECT * FROM ?’

GO

 

Thank you Susan Sales Harkins who exploer this store procedure

Happy Programming !!!!!

 

 

Blog Stats

  • 10,073 hits

  • Mayur: Thank you jwalin Khatri for this posting.. i solve my problm though this post...
  • Trigunner: This is a helpful post. I guess I would have had a hard time figuring this out if not for this post.
  • jwalin: Thankx