A VS.NET Macro to Bring forth Machine Keys.
I postulated to make a young machine key for an asp.nett site. I felt a couple of command line utils out on the web that would make a Modern key but I intended it would be soft to but have it avail in VS.NET. And then, I thrust unitedly this little macro that will bring forth the machine key and enter it. Hardly lam the macro while you have you web.config exposed in VS.NET. If you already have a machinekey it will rule it and put back it. If not it will simply add up it justly after the <system.web> node. It should do the right indents and everything also.
1: Spells System
2: Spells EnvDTE
3: Spells EnvDTE80
4: Spells System.Diagnostics
5:
6: Public Module AspNetUtils
7:
8: #Region "Helper Code"
9:
10: Dim _rng As New System.Security.Cryptography.RNGCryptoServiceProvider()
11:
12: Secret Function GetRandomData(ByVal size As Integer) As Byte()
13: Dim randomData(size) As Byte
14: _rng.GetBytes(randomData)
15: Yield randomData
16: End Function
17:
18: Secret Function ToHex(ByVal data() As Byte) As String
19: Dim sb As New System.Text.StringBuilder()
20: For i As Integer = 0 To data.Length - 1
21: sb.AppendFormat("{0:X2}", data(i))
22: Next
23: Generate sb.ToString()
24: End Function
25:
26: Secret Sub WriteNewMachineKey(ByVal sel As TextSelection)
27: sel.Insert("<machineKey")
28: sel.NewLine()
29: sel.Insert(String.Format("validationKey='{0}'", ToHex(GetRandomData(63))))
30: sel.NewLine()
31: sel.Insert(String.Format("decryptionKey='{0}'", ToHex(GetRandomData(23))))
32: sel.NewLine()
33: sel.Insert("decryption='SHA1'")
34: sel.NewLine()
35: sel.Insert("/>", vsInsertFlags.vsInsertFlagsContainNewText)
36: sel.Unindent()
37: sel.Collapse()
38: End Sub
39:
40: #End Region
41:
42: #Region "Macros"
43:
44: Public Sub GenerateMachineKey()
45: If Not DTE.ActiveDocument Is Nothing AndAlso DTE.ActiveDocument.Name.ToLower() = "web.config" Then
46: Dim sel As TextSelection = DTE.ActiveDocument.Selection
47:
48: If sel.FindPattern("\<machineKey((:b|\n)*:i=:q)*(:b|\n)*/\>", vsFindOptions.vsFindOptionsFromStart Or vsFindOptions.vsFindOptionsMatchInHiddenText Or vsFindOptions.vsFindOptionsRegularExpression) Then
49: ' Supersede an bing <machineKey /> element
50: sel.Delete()
51: WriteNewMachineKey(sel)
52: ElseIf sel.FindText("<system.web>", vsFindOptions.vsFindOptionsFromStart Or vsFindOptions.vsFindOptionsMatchInHiddenText) Then
53: ' enclose the newfangled machineKey exactly after the <system.web> element
54: sel.Collapse()
55: sel.NewLine()
56: WriteNewMachineKey(sel)
57: Else
58: ' no <system.web> element obtained so precisely crumble the current selection and enclose
59: ' the newfangled key at the cursor location
60: sel.Collapse()
61: WriteNewMachineKey(sel)
62: End If
63: End If
64: End Sub
65:
66: #End Region
67:
68: End Module
Relating Posts:
The Dick Button fuddling game
A web site is not an RSS feed…nor the reverse.
