為什麽這裏還是能看到明文的用戶名和密碼呢?
原因是因為:/p/securityswitch/downloads/list 我選擇的是 SecuritySwitch v4.2.0.0 - Binary.zip這個版本
1: 我們來看看測試項目
admin 文件夾,需要登錄之後,才能訪問。admin裏面的 login.aspx 可以訪問。整個admin文件夾都需要/a.aspx 或者是 /a.aspx。
下面我們開始用SecuritySwith來實現上面的
而妳的 baseSecureUri (基本安全網址) 是
然後這個時候妳訪問壹個需要/login.aspx?return=joey訪問的,那麽這個
頁面會跳轉到/login.aspx?return=joey
-->
<paths>
<add path="~/contact.aspx"/>
<add path="~/admin/login.aspx"/>
<add path="~/admin" />
<!--這裏的admin因為不僅是 admin 文件夾,而且還包含類似的 adminNews.aspx adminQQ.aspx 頁面"-->
<!--但是如果是 ~/admin/ 就是專門指admin文件夾-->
</paths>
</securitySwitch>
<!--SSL配置1結束—>
<appSettings />
<system.web>
<compilation debug="true">
</compilation>
<!-- 內置票據認證 start-->
<authentication mode="Forms">
<forms name="mycook" loginUrl="admin/login.aspx" protection="All" path="/" />
</authentication>
<!--SSL配置2 如果是 IIS <= 6.x, IIS 7.x + 經典模式-->
<httpModules>
<add name="SecuritySwitch" type="SecuritySwitch.SecuritySwitchModule, SecuritySwitch" />
</httpModules>
<!--SSL配置2結束-->
</system.web>
<!--SSL配置2 如果是IIS7.X + 集成模式-->
<!--<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
--><!-- for IIS 7.x + 集成模式 --><!--
<add name="SecuritySwitch" type="SecuritySwitch.SecuritySwitchModule, SecuritySwitch" />
</modules>
</system.webServer>-->
<!--如果是IIS7.X+集成模式 SSL配置2 結束—>
</configuration>
4: 由於用到了內置票據認證,所以要在 Global.asax添加以下代碼:
protected void Application_AuthenticateRequest(object SENDER, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket tiecket = id.Ticket;
string userData = tiecket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}
5: 後臺登陸界面 admin/login.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="web.admin.login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
用戶名:<asp:TextBox ID="txtUser" runat="server"></asp:TextBox><br />
密碼:<asp:TextBox ID="txtPass" runat="server"></asp:TextBox><br />
記住用戶名:<asp:CheckBox ID="chkUsername" runat="server" Checked="true"/>
<br />
<asp:Literal ID="litResult" runat="server"></asp:Literal>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" />
</form>
</body>
</html>
後臺登陸界面 cs admin/login.aspx.cs:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace web.admin
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string username = txtUser.Text;
string pass = txtPass.Text;
bool ischeck=chkUsername.Checked;
if (username=="admin" && pass=="admin")
{
SaveLogin(username, ischeck);
}
}
private void SaveLogin(string username, bool ischeck)
{
HttpCookie cook;
string strReturnURL;
string roles = "admin";//將用戶的usernameid,保存到cookies,身份是 admin 身份
//要引用 using System.Web.Security;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, username, DateTime.Now, DateTime.Now.AddMinutes(30), ischeck, roles);
cook = new HttpCookie("mycook");//對應webconfig裏面的 驗證裏面的 forms name="mycook"
cook.Value = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(cook);
strReturnURL = Request.Params["ReturnUrl"];
if (strReturnURL != null)
{
Response.Redirect(strReturnURL);
}
else
{
Response.Redirect("default.aspx");
}
}
}
}