Asp.net2.0中单域名多服务器的单点登陆(Single SignOn)

 

问题描述:
有一主域名www.wow52.cn,跟AAA.wow52.cn,BBB.wow52.cn...等一系列子域名,各对应一个子系统,各系统多采用Asp.net 2.0技术实现,并且分布在不同的服务器上面,现在要求在这些系统中实现单点登陆.

方法如下
在个站点(子系统)的Web.config文件中增加以下2个节点
<system.web>

  <machineKey validationKey="155027D0BE19AF1034CD97AF23C26D90B0F34AFAD33B34D292086A2EF974543C5D940D8B97F569E4EF3B288A2DD368383F75478BAE33C67C242C027A13C1EB5B"
           decryptionKey="5F1C24D664A14AB7862FCE8F6A98E9E274DC1E21E6BFB060"
           validation="SHA1"/>
  
  <authentication mode="Forms">
   <forms domain=".wow52.cn"
    defaultUrl="index.html"
    name=".xxx.comUSERNAMECOOKIE"
    protection="All"
    loginUrl="/UserLogin.aspx" />
  </authentication>

</system.web>

用于生成节点<machinekey>的代码如下
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();
        }
    }
}
将以上代码编译后得到 hashconfigcs.exe 文件
在命令行输入 hashconfigcs.exe 24 64 key.txt
则 key.txt文件的内容即为<machineKey >节点

说明:
Asp.net 中的Forms表单身份验证是基于Cookie的,不过对Cookie数据按一定算法进行了加密与签名,默认情况下是用Machine.config  文件中的<machinekey>节点配置的信息(密匙,跟算法)来完成的,而在Machine.config 中的<machinekey>采用AutoGenerate 选项 因此不同服务器上的密匙基本不同,所以要手动来指定.

原文地址:https://www.cnblogs.com/wdfrog/p/1103480.html