1、使用Login控件

使用Login控件

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

[code]
<%@ 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>
[/code]

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

2011-4-29 22:45 danny

2、自动隐藏已验证用户的Login控件
LoginMaster.master
[code]
<%@ 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>
[/code]

LoginContent.aspx
[code]
<%@ 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>
[/code]

3、使用模板定制Login控件
LoginTemplate.aspx
[code]
<%@ 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>
[/code]

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

Web.Config
[code]
<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>
[/code]

LoginCustom.aspx
[code]
<%@ 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>
[/code]

2011-5-3 11:10 danny
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值