复制代码 代码如下:
<authentication mode="Forms">
<forms loginUrl="manager/Login.aspx" defaultUrl="manager/default.aspx" protection="All" timeout="60" />
</authentication>
复制代码 代码如下:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="Admin" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
复制代码 代码如下:
if (!snCheckCode.CheckSN(txt_ValidateCode.Text))
{
snCheckCode.Create();
Utility.ShowMessage("校验码错误!");
return;
}
string strUserName = txt_Username.Text.Trim();
string md5Pwd = Helper.MD5ForPHP(Helper.MD5ForPHP(txt_Password.Text));
lc_admin admin = null;
bool logined = false;
using (var context = new dbEntities())
{
admin = context.tb_admin.Where(n => n.username == strUserName).FirstOrDefault();
if (admin != null)
{
if (admin.checkadmin != "true")
{
snCheckCode.Create();
Utility.ShowMessage("抱歉,该账号被禁止登录!");
return;
}
if (admin.password == md5Pwd)
{
// Update Admin Info
admin.loginip = Request.UserHostAddress.ToString();
admin.logintime = CndingUtility.DateTimeToUnixTimeStamp(DateTime.Now);
context.SaveChanges();
logined = true;
}
}
}
if (logined)
{
// Login
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
admin.id.ToString(),
DateTime.Now,
DateTime.Now.AddMinutes(60),
false,
"Admin",
FormsAuthentication.FormsCookiePath
);
string hashTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
HttpContext.Current.Response.Cookies.Add(userCookie);
if (Request["ReturnUrl"] != null)
{
Response.Redirect(HttpUtility.HtmlDecode(Request["ReturnUrl"]));
}
else
{
Response.Redirect("/manager/default.aspx");
}
}
else
{
snCheckCode.Create();
CndingUtility.ShowMessage("用户名或密码不正确!");
}
复制代码 代码如下:
public static string MD5ForPHP(string stringToHash)
{
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] emailBytes = Encoding.UTF8.GetBytes(stringToHash.ToLower());
byte[] hashedEmailBytes = md5.ComputeHash(emailBytes);
StringBuilder sb = new StringBuilder();
foreach (var b in hashedEmailBytes)
{
sb.Append(b.ToString("x2").ToLower());
}
return sb.ToString();
}
复制代码 代码如下:
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
int adminId = -1;
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = identity.Ticket;
string userData = ticket.UserData;
if (userData == "Admin")
{
// To do something
}
}
复制代码 代码如下:
<authentication mode="Forms">
<forms cookieless="UseCookies" loginUrl="manager/Login.aspx" defaultUrl="manager/default.aspx" protection="All" timeout="60" />
</authentication>
新闻热点
疑难解答
图片精选