asp.net 单点登录

1、首先在建2个项目,在web.config里的<system.web>节点下添加

<machineKey validationKey="282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760ADF21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141" decryptionKey="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099" validation="SHA1"/>

两个项目的设置必须相同

2、  把配置文件的<authentication mode="Windows"/>验证改成Form验证

<authentication mode="Forms">
   <forms loginUrl="Login.aspx" name="随便写,但要保持2个项目的值相同,默认是.ASPXAUTH"></forms>
  </authentication>
  <authorization>

<!-- 所有用户都可以访问,改成<deny users="?"></deny>表示禁止匿名用户登录,如果对Form验证不了解,参考其他文章 -->
   <allow users="*"/> 
  </authorization>

3、在其中一个项目建立登陆页面login.aspx,拖一个文本框和按钮

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="登陆" onclick="Button1_Click" />

后台代码为:

 

代码
   
     
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class login : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{

}
protected void Button1_Click( object sender, EventArgs e)
{
// 登录用户名:对应 Web.config 中 <allow users="Admin" … /> 的 users 属性
System.Web.Security.FormsAuthenticationTicket tk = new System.Web.Security.FormsAuthenticationTicket( 1 ,
this .TextBox1.Text, System.DateTime.Now, System.DateTime.Now.AddDays( 30 ), false , " 测试用户数据|dddd " );
// 用户数据:可用 ((System.Web.Security.FormsIdentity)User.Identity).Ticket.UserData 获取
string str = System.Web.Security.FormsAuthentication.Encrypt(tk); // 加密身份验票

// 声明一个 Cookie,名称为 Web.config 中 <forms name=".APSX" … /> 的 name 属性,对应的值为身份验票加密后的字串
System.Web.HttpCookie ck = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, str);

// 指定 Cookie 为 Web.config 中 <forms path="/" … /> path 属性,不指定则默认为“/”
ck.Path = System.Web.Security.FormsAuthentication.FormsCookiePath;
// 此句非常重要,少了的话,就算此 Cookie 在身份验票中指定为持久性 Cookie ,也只是即时型的 Cookie 关闭浏览器后就失效;因此上面我说:我是真的还不知在身份验票中指定为持久性 Cookie 有何用。
ck.Expires = System.DateTime.Now.AddYears( 100 );
Response.Cookies.Add(ck);
// 添加至客户端


Response.Redirect(
" Default.aspx " );
}
}

 

 

 

3、在Default.aspx页面中,加载事件为:

        string cookieName = FormsAuthentication.FormsCookieName;

        Response.Write(cookieName);

        HttpCookie authCookie = Context.Request.Cookies[cookieName];

        if (null == authCookie)

        {

            return;

        }

        FormsAuthenticationTicket authTicket = null;

        try

        {

            authTicket = FormsAuthentication.Decrypt(authCookie.Value);

            Response.Write("</br>"+authCookie.Value);

        }

        catch (Exception ex)

        {

            return;

        }

        if (null == authTicket)

        {

            return;

        }



        FormsIdentity id = new FormsIdentity(authTicket);

        String[] roles = id.Ticket.UserData.Split('|'); //读出在登录时设置的角色列表。 



        foreach (string role in roles)

        {

            Response.Write("</br>"+role);

        }

        System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);

        Context.User = principal;//将验证信息附加到当前用户上下文。

        Response.Write("</br>" + User.Identity.Name+"</br>"+User.GetType());
显示了获取此次登录保存在Cookie中的用户信息(已近加密)。

4、同样在第二个项目的某个页面获取登录的Cookie信息,

User.Identity.Name是登录名,从数据库获取信息,放入sesstion中,进行相应操作。

5、退出系统用

System.Web.Security.FormsAuthentication.SignOut();
        Response.Redirect("Default.aspx");即可。

 

你可能感兴趣的:(asp.net)