달력

32024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
L4 스위치 등 로드 밸런싱 서버 팜에서 포스트 백 된 aspx 페이지가 ViewState 유효성 을 위반하는 문제.

즉 서버팜에 있는 하나의 서버에서 페이지를 요청한 후 액션이 일어났을때 다른 서버의 동일 페이지로 포스트백되는 현상. 요청서버와 포스트백되는 서버가 다르기 때문에 요청서버에서 생성된 ViewState를 포스트백 되는 서버에서 해석하지 못해 에러가 노출된다.

이때 ViewState를 암,복호화 하는 키값을 각 서버마다 동일하게 "박아"버리면 문제를 해결할 수 있다.

다음과 같은 KeyMaker 어플리케이션을 작성후 키값을 생성시키고, 각 서버의 machine.config의 <MachineKey/> 항목을 업데이트 하라.

FIX: "The View State Is Invalid for This Page and Might Be Corrupted" Error Message in ASP.NET

SYMPTOMS
You may receive the following error message in ASP.NET:
The View State is invalid for this page and might be corrupted
You may receive this error message under the following circumstances: ?

Scenario 1: Your ASP.NET application is hosted on a Web farm. A user receives a page that is served by one server but is posted to another server in that Web farm. -or-

?Scenario 2: You modify your pages, which causes the shadow, copied files in the Temporary ASP.NET files folder to be regenerated. A user has a copy of the page that was requested before this change, and the user posts the page after the files in that folder were regenerated.

NOTE: In Scenario 1, verify that the machine keys match for all of the computers in the Web farm before you apply the hotfix that is listed in the "Resolution" section. This error message occurs if the machine keys do not match. If the machine keys do not match, generate an identical machine key for use in all Web servers before you apply the patch. For additional information about how to generate these machine keys, click the article numbers below to view the articles in the Microsoft Knowledge Base:

313091 HOW TO: Create Keys by Using Visual Basic .NET for Use in Forms Authentication
312906 HOW TO: Create Keys by Using Visual C# .NET for Use in Forms Authentication

CAUSE
The case-sensitive value that the TemplateSourceDirectory property of a page returns is used to create and to validate the ViewState property for that page. The value of this property for a page depends on the case-sensitive URL that the first user for that page requested. This value is reused for the remaining requests for that page until that page is recompiled. When the page is recompiled, the TemplateSourceDirectory property is re-initialized. If the new value (which is case-sensitive) differs from the previous value, the ViewState validation from the existing clients fails.

How to create keys by using Visual C# .NET for use in Forms authentication

SUMMARY
This article describes how to create keys to use for encryption, decryption, and validation of Forms authentication cookie data. You can use the keys that you create in this article for the validationKey and decryptionKey attributes of the <machineKey> section in the <system.web> element in the Machine.config file.

Create the project
Create a Visual C# .NET console application: 1. Start Visual Studio .NET.
2. On File menu, point to New, and then click Project.
3. Under Project Types, click Visual C# Projects.
4. Under Templates, click Console application.
5. Name the project HashConfigCs.
6. Click OK.

Write the code to generate the keys
The following code reads two arguments that are passed from the command line: ?The first argument is the number of bytes that is used to create the decryptionKey attribute.
?The second argument is the number of bytes that is used to create the validationKey attribute.
The code uses a random number generator to create a random number of bytes based on the command-line arguments. After the random bytes are created, the bytes are formatted into a hexadecimal string that is suitable for use in the .config files.

Note The hexadecimal string that is created is twice the size of the value that is passed on the command line. For example, if you specify 24 bytes for a key, the resulting string is 48 bytes in length after the conversion. The valid values for decryptionKey is 8 or 24. This creates a 16 byte key for Data Encryption Standard (DES) or a 48 byte key for Triple DES, respectively. Valid values for validationKey are 20 to 64. This creates keys from 40 to 128 bytes in length. The output from the code is an entire <machineKey> element that you can copy and paste into a Machine.config file.

Add the following code to a .cs file:

using System;
using System.Text;
using System.Security.Cryptography;

namespace Crypto
{
public class KeyCreator
{
public static void Main(String[] args)
{
String[] commandLineArgs = System.Environment.GetCommandLineArgs();
string decryptionKey = CreateKey(System.Convert.ToInt32(commandLineArgs[1]));
string validationKey = CreateKey(System.Convert.ToInt32(commandLineArgs[2]));

Console.WriteLine("<machineKey validationKey=\"{0}\" decryptionKey=\"{1}\" validation=\"SHA1\"/>", validationKey, decryptionKey);
}

static String CreateKey(int numBytes)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[numBytes];

rng.GetBytes(buff);
return BytesToHexString(buff);
}

static String BytesToHexString(byte[] bytes)
{
StringBuilder hexString = new StringBuilder(64);

for (int counter = 0; counter < bytes.Length; counter++)
{
hexString.Append(String.Format("{0:X2}", bytes[counter]));
}
return hexString.ToString();
}
}
}

Generate the hashes
Now you can compile the application.

Run the application from a command prompt by passing in two integer values that are the size of the decryption and the validation keys. For example, if you named the console application HashConfigCs.exe, type the following syntax from the command line in the Bin\debug directory of the application:
hashconfigcs.exe 24 64
You can expect the application to return output that is similar to the following output:

<machineKey
validationKey="21F090935F6E49C2C...."
decryptionKey="261F793EB53B7615....."
validation="SHA1"/>

Note Because the code is using a random number generator, the output is different each time.

Update the configuration file
1. Locate the Machine.config file.
2. Locate the <system.web> section in the configuration file.
3. Replace the <machineKey> section with the output from the console application. If the <machineKey> section does not exist, create it.
4. Save the configuration file.
5. Restart IIS on all servers in the Web farm for the Machine.config changes to take effect.

Troubleshooting
Make sure that the <machineKey> section has identical, explicit keys (that is, do not use the AutoGenerate option for attributes in the <machineKey> section) across the Web farm in the following scenarios: ?When you use Forms authentication.
?When you run session state in StateServer mode.
?When you want ViewState to be available across a Web farm because the enableViewStateMAC attribute is set to True by default.

APPLIES TO
?Microsoft ASP.NET (included with the .NET Framework) 1.0
?Microsoft Visual C# .NET 2002 Standard Edition
?Microsoft ASP.NET (included with the .NET Framework 1.1)
?Microsoft Visual C# .NET 2003 Standard Edition
Posted by tornado
|