Autenticação via form (modo simples)

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > ASP Controls >

Autenticação via form (modo simples)

Previous pageReturn to chapter overviewNext page

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

  <div>

      <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Login.aspx">Login</asp:HyperLink>

      <br />

      <br />

  </div>

  <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/Protegida.aspx">Protegida</asp:HyperLink>

  <br />

  <br />

  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

  <br />

  </form>

</body>

</html>

 

Default.aspx.cs

 

using System;

using System.Web;

 

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

{

  protected void Page_Load(object sender, EventArgs e)

   {

      // usuário tá autenticado?

      if (HttpContext.Current.User.Identity.IsAuthenticated)

           Label1.Text = "nome do usuário autenticado: " + HttpContext.Current.User.Identity.Name;

      else

           Label1.Text = "não autenticado";

   

   }

}

 


 

Web.config

 

<?xml version="1.0"?>

<!--

 For more information on how to configure your ASP.NET application, please visit

 http://go.microsoft.com/fwlink/?LinkId=169433

-->

<configuration>

 <system.web>

 

   <!-- modo forms e a login.aspx é quem vai validar -->

         <authentication mode="Forms">

                 <forms name="Protegida" loginUrl="Login.aspx"/>

         </authentication>

   

         <customErrors>

         </customErrors>

         <compilation debug="true" targetFramework="4.0"/>

 </system.web>

 

 <!-- usuarios sem autenticacao nao podem logar na protegida -->

 <location path="Protegida.aspx">

         <system.web>

                 <authorization>

                         <deny users="?"/>

                 </authorization>

         </system.web>

 </location>

</configuration>

 


 

Protegida.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Protegida.aspx.cs" Inherits="Protegida" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

  <div>

   

      <asp:Label ID="Label1" runat="server" Text="Você está logado!"></asp:Label>

      <br />

      <br />

      <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Log-out</asp:LinkButton>

   

  </div>

  </form>

</body>

</html>

 

Protegida.aspx.cs

 

using System;

using System.Web.Security;

 

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

{

  protected void Page_Load(object sender, EventArgs e)

   {

   }

 

  protected void LinkButton1_Click(object sender, EventArgs e)

   {

      FormsAuthentication.SignOut();

   }

}

 


 

Login.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

  <p>

      <asp:TextBox ID="txtLogin" runat="server"></asp:TextBox>

  </p>

  <p>

      <asp:TextBox ID="txtSenha" runat="server"></asp:TextBox>

  </p>

  <p>

      <asp:Button ID="btnEntrar" runat="server" onclick="btnEntrar_Click"

          Text="Entrar" />

  </p>

  <p>

      <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>

  </p>

  </form>

</body>

</html>

 

Login.aspx.cs

 

using System;

using System.Web.Security;

 

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

{

  protected void Page_Load(object sender, EventArgs e)

   {

 

   }

  protected void btnEntrar_Click(object sender, EventArgs e)

   {

      if (txtLogin.Text == "jr" && txtSenha.Text == "123")

       {

          // autorizado

          FormsAuthentication.RedirectFromLoginPage(txtLogin.Text, true);

       }

      else

           lblMsg.Text = "Usuario ou senha invalidos";

   }

}

 

Observações

 

Você pode usar também em conjunto os controles:

LoginStatus

LoginName

LoginView