在做一个ASP.NET的项目,想留一个超级管理员的账号。
如果这个超级管理员的用户名是固定的(例如administrator),这样会不安全;所以想用下面的这种方式生成一个长字符串的用户名,而且每小时变化一次。
用户名的组成:年+SuperAdmin+总天数+小时à进行MD5加密。因此,用户名每个小时都会变化。
using System;
using System.Text;
using System.Security.Cryptography;
namespace CKI.JsonServer.Models
{
public class SuperAdministratorHelper
{
public static string GetName()
{
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int day = DateTime.Now.Day;
int hour = DateTime.Now.Hour;
int SumDays = GetDayOfYear(month) + day;
if (month > 2)
{
if ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0)
{
SumDays++;
}
}
string sa = year + "SuperAdmin" + SumDays.ToString("#000") + hour.ToString("#00");
return MD5(sa);
}
public static string MD5(string str)
{
byte[] result = Encoding.Default.GetBytes(str);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
string strMD5 = BitConverter.ToString(output).Replace("-", "");
return strMD5;
}
private static int GetDayOfYear(int month/*取值范围:1至12*/)
{
int SumDays = 0;
if (month <= 0) return SumDays;
switch (month - 1)
{
case 11: SumDays += 30; break;
case 10: SumDays += 31; break;
case 9: SumDays += 30; break;
case 8: SumDays += 31; break;
case 7: SumDays += 31; break;
case 6: SumDays += 30; break;
case 5: SumDays += 31; break;
case 4: SumDays += 30; break;
case 3: SumDays += 31; break;
case 2: SumDays += 28; break;
case 1: SumDays += 31; break;
default: break;
}
SumDays += GetDayOfYear(month - 1);
return SumDays;
}
}
}原文:http://lsieun.blog.51cto.com/9210464/1742913