由于頁面MyPassport.aspx使用了.NET Passport驗證,因此需要在該頁面中引入名字空間System.Web.Security和System.Security.Principal等。引入名字空間的程序代碼如下:
using System.Security;
using System.Security.Principal;
using System.Web.Security;
using System.Security.Policy;
頁面MyPassport.aspx使用函數UserLogin()驗證用戶是否登錄。如果用戶已經登錄網站或應用程序,則顯示相應操作,否則在頁面MyPassport.aspx顯示Passport驗證登錄入口。函數UserLogin()的程序代碼如下:
private void UserLogin()
{ //使用Passport驗證的身份ID
PassportIdentity pIdent = (PassportIdentity)Context.User.Identity;
if(pIdent.IsAuthenticated == true)
{ //顯示用戶登錄成功信息
Response.Write("用戶登錄成功。!");
Response.Write("<br>" + "用戶ID為:" + pIdent.HexPUID);
}
else
{ //顯示用戶登錄入口
Response.Write("用戶還沒有登錄,請單擊下面的登錄按鈕:");
Response.Write(
pIdent.LogoTag2(null,600,true,null,1033,false,null,0,false));
}
}
運行效果
設置頁面MyPassport.aspx為應用程序的起始頁面。按F5鍵運行,出現如圖12-4所示的初始界面。
圖12-4 頁面MyPassport.aspx的初始界面
文章來源于領測軟件測試網 http://www.kjueaiud.com/