A VS.NET Macro to Generate Machine Keys.
Also see: xClaims and Microformats
I needed to create a new machine key for an asp.net site. I found a couple of command line utils out on the web that would create a new key but I thought it would be easier to just have it avail in VS.NET. So, I threw together this little macro that will generate the machine key and insert it. Just run the macro while you have you web.config open in VS.NET. If you already have a machinekey it will find it and replace it. If not it will just add it right after the <system.web> node. It should do the proper indents and everything too.
1: Imports System
2: Imports EnvDTE
3: Imports EnvDTE80
4: Imports System.Diagnostics
5:
6: Public Module AspNetUtils
7:
8: #Region "Helper Code"
9:
10: Dim _rng As New System.Security.Cryptography.RNGCryptoServiceProvider()
11:
12: Private Function GetRandomData(ByVal size As Integer) As Byte()
Also see: Blogging and Newspapers, a Lesson in How Not to Brand and Market
Also see: Generics and .NET
Also see: Fix ReturnUrl When Sharing Forms Authentication with Multiple Web Applications
Also see: Single source code base for Silverlight and WPF solutions
Also see: ASP.NET MVC in CodePlex and Extensible Unit Testing
Also see: Foundations for Structured Programming with GADTs
Also see: I’ve finally settled into my new position on the Internet Explorer team…
Also see: Single source code base for Silverlight and WPF solutions
Also see: I’ve finally settled into my new position on the Internet Explorer team…
Also see: Is this ethical - Part 2
Also see: LearnExpression.com is live.
Also see: ReflectionTypeLoadException
Also see: Dare Obasanjo on C# Anonymous Types
Also see: Language parsing and compiler design doesn’t have to be hard, but boy this book really sucks!
Also see: LINQ - The Uber FindControl
Also see: Is this ethical - Part 2
Also see: Infrequent blogging
Also see: Gilad Bracha: Cutting out Static
Also see: Java Concurrency, another series on its issues
Also see: Applied Metamodelling: A Foundation for Language Driven Development
Also see: My Presidential Endorsement:
Also see: Adding some Project Distributor Client Tools.
13: Dim randomData(size) As Byte
14: _rng.GetBytes(randomData)
15: Return randomData
16: End Function
17:
18: Private 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: Return sb.ToString()
24: End Function
25:
Also see: Bloggers in the Mavs Locker Room ?
Also see: Dare Obasanjo on C# Anonymous Types
Also see: xClaims and Microformats
Also see: Using IronPython for Dynamic Expressions.
Also see: Single source code base for Silverlight and WPF solutions
Also see: Silverlight 2 Developer Poster
Also see: C# 3.0 Lambdas and Type Inference
Also see: Mix 08 Sessions Published
Also see: Chicago geek dinner 11/22
Also see: Turning bitboards from potential moves into legal moves, pawn moves, and conditional rules.
Also see: Mix 08 Sessions Published
Also see: From C# to Java: Part 3
Also see: Introducing Microsoft Tagspace
Also see: My Presidential Endorsement:
Also see: Using IronPython for Dynamic Expressions.
Also see: Is this ethical - Part 2
Also see: Implied tags in the IE HTML parser and how that can be interesting.
Also see: Silverlight 2 Developer Poster
Also see: Uniqueness Typing Simplified
Also see: UI design
Also see: Parallels adds “Express Windows Installation”
Also see: Parsing things you don’t understand
Also see: Parallels adds “Express Windows Installation”
Also see: Be my Support Group
Also see: Important changes to the BASE element for IE 7
Also see: C# 3.0 Lambdas and Type Inference
Also see: The Exception Model
Also see: Blogging and Newspapers, a Lesson in How Not to Brand and Market
Also see: Applied Metamodelling: A Foundation for Language Driven Development
Also see: Important changes to the BASE element for IE 7
Also see: Snippet Compiler update
Also see: Parallels adds “Express Windows Installation”
Also see: Sliced Bananas On Opaque Data
Also see: Bloggers in the Mavs Locker Room ?
Also see: Bloggers in the Mavs Locker Room ?
26: Private 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: ' Replace an existing <machineKey /> element
50: sel.Delete()
51: WriteNewMachineKey(sel)
52: ElseIf sel.FindText("<system.web>", vsFindOptions.vsFindOptionsFromStart Or vsFindOptions.vsFindOptionsMatchInHiddenText) Then
53: ' insert the new machineKey just after the <system.web> element
54: sel.Collapse()
55: sel.NewLine()
56: WriteNewMachineKey(sel)
57: Else
58: ' no <system.web> element found so just collapse the current selection and insert
59: ' the new 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
http://weblogs.asp.net/dfindley/archive/2007/02/05/a-vs-net-macro-to-generate-machine-keys.aspx
