六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 35|回复: 0

1、使用Login控件

[复制链接]

升级  57%

115

主题

115

主题

115

主题

举人

Rank: 3Rank: 3

积分
371
 楼主| 发表于 2013-1-26 14:10:39 | 显示全部楼层 |阅读模式
使用Login控件

1、Login控件概览
加密文件:
Secret.aspx  (SecretFiles\Secret.aspx)

<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>Secret</title></head><body>    <form id="form1" runat="server">    <div>        <h1>            This Page is Secret!</h1>    </div>    </form></body></html>

希望通过密码身份验证来保护页面Secret.aspx,需要对应用程序做两个配置更改:身份验证和授权设置。
默认是通过Windows身份验证。要改为启用Forms身份验证才能用Login控件
Web.config
<configuration> <system.web>   <authentication mode="Forms"/> </system.web></configuration>

2011-4-29 22:45 danny

2、自动隐藏已验证用户的Login控件
LoginMaster.master
<%@ Master Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <style type="text/css">        html        {            background-color: Silver;        }        .content        {            margin: auto;            width: 650px;            border: solid 1px black;            background-color: White;            padding: 10px;        }        .login        {            font: 10px Arial,Sans-Serif;            margin-left: auto;        }        .login input        {            font: 10px Arial,Sans-Serif;        }    </style>    <title>My Website</title></head><body>    <form id="form1" runat="server">    <div class="content">        <asp:Login ID="Login1" Orientation="Horizontal" VisibleWhenLoggedIn="false" DisplayRememberMe="false"            TitleText="" CssClass="login" runat="server" />        <hr />        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">        </asp:ContentPlaceHolder>    </div>    </form></body></html>

LoginContent.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/LoginMaster.master" %><script runat="server"></script><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">    <h1>        Welcome to our WebSite!</h1></asp:Content>

3、使用模板定制Login控件
LoginTemplate.aspx
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <style type="text/css">        .loginError        {            color: Red;            font: bold 14px Arial,Sans-Serif;        }    </style>    <title>Login Template</title></head><body>    <form id="form1" runat="server">    <div>        <asp:Login ID="Login1" runat="server">            <LayoutTemplate>                <asp:Label ID="FailureText" EnableViewState="false" CssClass="loginError" runat="server" />                <br />                <asp:Label ID="lblUserName" AssociatedControlID="UserName" Text="User Name:" runat="server" />                <br />                <asp:TextBox ID="UserName" runat="server" />                <br />                <br />                <asp:Label ID="lblPassword" AssociatedControlID="Password" Text="Password:" runat="server" />                <br />                <asp:TextBox ID="Password" TextMode="Password" runat="server" />                <br />                <br />                <asp:Button ID="btnButton" Text="Login" CommandName="Login" runat="server" />            </LayoutTemplate>        </asp:Login>    </div>    </form></body></html>

4、使用Login控件执行自定义身份验证
默认情况下,Login控件使用Asp.net Membership来验证用户名和密码。如果要修改此默认行为,那么可以对Login控件的Authenticate事件进行处理。

Web.Config
<configuration>  <system.web>  <authentication mode="Forms">      <forms>        <credentials passwordFormat="Clear">          <user name="Bill" password="way"/>          <user name="Danny" password="way"/>        </credentials>              </forms>  </authentication>  </system.web></configuration>

LoginCustom.aspx
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)    {        string userName = Login1.UserName;        string password = Login1.Password;        e.Authenticated = FormsAuthentication.Authenticate(userName, password);    }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>Login Custom</title></head><body>    <form id="form1" runat="server">    <div>        <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate" />    </div>    </form></body></html>

2011-5-3 11:10 danny
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表