ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Forms Authentication 速记[20090203修正]

Forms Authentication 速记[20090203修正]

原创 Linux操作系统 作者:kirinri 时间:2009-02-02 14:22:21 0 删除 编辑

一个Web Site, Login page 是Default.aspx, login Succeed 则转到Succeed.aspx.没有通过Form. Authentication则被重定向到Login page.

20090203修正内容:web.config的session&forms标签,页面中session中的测试。在IE7下cookie可用|禁用时session&form. authentication测试通过。

web.config:

xml version="1.0"?>

<configuration>

  <system.web>

    <compilation debug="true"/>

    <!--解决当浏览器端禁用Cookie-->

    <sessionState cookieless="AutoDetect"/>

    <authentication mode="Forms">

      <forms name="CookieName" loginUrl="Default.aspx" protection="All" cookieless="AutoDetect">forms>

      <!--loginUrl为登录面URL,如果没有身份验证Cookie,客户端将被重定向到此URL-->

    authentication>

 

    <authorization>

      <deny users="?"/>

    authorization>

 

    <customErrors mode="On" defaultRedirect="GenericErrorPage.htm">

      <error statusCode="403" redirect="" />

      <error statusCode="404" redirect="" />

    customErrors>

  system.web>

configuration>

 

 

Default.aspx

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        string TeValue = this.TextBox1.Text;

        string errorMe = string.Empty;

 

        try

        {

            if (!TeValue.Equals(String.Empty))

            {

                System.Web.Security.FormsAuthentication.SetAuthCookie("myName", false);

                Response.Redirect("Succeed.aspx",false);

                Session.Add("mySession", "mySessionView");

            }

            else

            {

                Response.Write("You are wrong.");

            }

 

        }

        catch (Exception ee)

        {

            errorMe = ee.Message;

        }

 

    }

 

}

 

Succeed.aspx

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class Succeed : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Response.Write("on Succeed page --->>>" + Session["mySession"]);

        Boolean userAuth = User.Identity.IsAuthenticated;

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        System.Web.Security.FormsAuthentication.SignOut();

        Response.Redirect("Default.aspx");

 

    }

}

 

 

  页面&Cookie Screenshot:

1.JPG

2.JPG

3.JPG

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-545034/,如需转载,请注明出处,否则将追究法律责任。

下一篇: SecureString 速记
请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    257
  • 访问量
    499422