ASP.Net/Login Security/LoginStatus

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Displaying the username of the authenticated user

   <source lang="csharp">

<%@ Page Language="VB" %>

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

   <title>Login or Logout</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:LoginStatus ID="LoginStatus1" Runat="server" />

<asp:LoginName ID="LoginName1" Runat="server" Font-Bold="True" Font-Size="Large"/>

   </form>

</body> </html>

</source>
   
  


Login and logout features of the LoginStatus control

   <source lang="csharp">

<%@ Page Language="VB" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Login or Logout</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:LoginStatus ID="LoginStatus1" Runat="server" />
   </form>

</body> </html>

</source>
   
  


outputs the name of the currently logged user as authenticated by the ASP.NET application

   <source lang="csharp">

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

   Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Welcome aboard</title>

</head> <body>

       <form id="form1" runat="server">
<asp:loginname ID="Loginname1" runat="server" FormatString="Welcome, {0}" /> <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutText="Log off" LogoutAction="Refresh" />
           <asp:Label runat="Server" ID="Msg" />
       </form>

</body> </html> File: Default.aspx.cs 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 Default : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
       if (User.Identity.IsAuthenticated)
           Msg.Text = "Authenticated";
       else
           Msg.Text = "Not Authenticated";
   }

}

</source>
   
  


Welcome page

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Welcome_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:LoginStatus ID="LoginStatus1" runat="server" />
       <asp:LoginView ID="LoginView1" runat="server">
           <LoggedInTemplate>
               Welcome
               <asp:LoginName ID="LoginName1" runat="server" />
               
<asp:HyperLink ID="hlChangePW" NavigateUrl="ChangePW.aspx" runat="server">Change Password </asp:HyperLink>   <asp:HyperLink ID="hlCreateUser" NavigateUrl="CreateAccount.aspx" runat="server">Create User </asp:HyperLink>
<asp:HyperLink ID="hlManageRoles" NavigateUrl="ManageRoles.aspx" runat="server">Manage Roles </asp:HyperLink>
</LoggedInTemplate> <AnonymousTemplate> You are not logged in. </AnonymousTemplate> </asp:LoginView> <asp:HyperLink ID="hlProfile" NavigateUrl="ProfileInfo.aspx" runat="server">Profile Information </asp:HyperLink>
<asp:ListBox ID="lbBooks" runat="server" /> <asp:Panel ID="pnlInfo" Runat="server" Visible="False"> <asp:Label ID="lblFullName" Runat="server" Text="Full name unknown"/> <asp:Label ID="lblPhone" Runat="server" Text="Phone number unknown"/> <asp:Label ID="lblBirthDate" Runat="server" Text="Birthdate unknown"/> </asp:Panel>
   </form>

</body> </html> File: Default.aspx.cs 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 Welcome_aspx : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
   if ( ! Profile.IsAnonymous )
   {
     this.pnlInfo.Visible = true;
     this.lblFullName.Text = Profile.firstName + " " + Profile.lastName;
     this.lblPhone.Text = Profile.phoneNumber;
     this.lblBirthDate.Text = Profile.birthDate.ToShortDateString();
     this.lbBooks.Items.Clear();
   }
   else
   {
     this.pnlInfo.Visible = false;
   }
   if (Profile.MyFlag != null)
   {
     foreach (string bookName in Profile.MyFlag)
     {
       this.lbBooks.Items.Add(bookName);
     }
   }
 }

} File: Web.Config <?xml version="1.0"?> <configuration>

 <connectionStrings>
   <remove name="LocalSqlServer"/>
   <add name="LocalSqlServer" connectionString="data source=.\SqlExpress;Integrated Security=SSPI;Initial Catalog=aspnetdb"/>
 </connectionStrings>
 <system.web>
   <anonymousIdentification enabled="true" />
   <authentication mode="Forms"/>
   <membership defaultProvider="AspNetSqlMembershipProvider"/>
   <roleManager enabled="True" defaultProvider="AspNetSqlRoleProvider"/>
   <compilation debug="true"/>
   <profile enabled="True" defaultProvider="AspNetSqlProfileProvider">
     <properties>
       <add name="lastName" />
       <add name="firstName" />
       <add name="phoneNumber" />
       <add name="birthDate" type="System.DateTime"/>
       <add name="MyFlag" allowAnonymous="true" 
        type="System.Collections.Specialized.StringCollection"  />
     </properties>
   </profile>
 </system.web>

</configuration>

</source>