using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ctti.Data;
using System.Web.Security;
namespace CttiDemo
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void uxAuthenticate_Click(object sender, EventArgs e)
{
//authenticate the user against the database
var db = new CTTIEntities1();
var auth = db.Authentications.SingleOrDefault(a => a.Username == uxUserId.Text
&& a.Password == uxPassword.Text);
if(auth != null)
{
var student = string.Format("{0} {1}", auth.Student.FirstName,
auth.Student.LastName);
//Redirect using the FormsAuthentication class
FormsAuthentication.RedirectFromLoginPage(student, false);
}
}
}
}
-----------------------------------------------------------------
//~/students/web.config
<?xml version="1.0"?>
<configuration>
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
</configuration>
----------------------------------------------------------------------
//~/web.config
//add <authentication> under <system.web>
<system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Login.aspx" />
    </authentication>
    <compilation debug="true" targetFramework="4.6.1">
      <assemblies>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.6.1" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
  </system.web>
----------------------------------------------------------------------------
//site.master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CttiDemo
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                uxWelcome.Text = "Welcome " + Page.User.Identity.Name;
                uxLogin.Text = "Logout";
                uxLogin.NavigateUrl = "~/Default.aspx";
            }
            else
            {
                uxWelcome.Text = string.Empty;
                uxLogin.Text = "LogIn";
                uxLogin.NavigateUrl = "~/Login.aspx";
            }
        }
    }
}
-----------------------------------------------------------------------
//site.master.aspx
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="CttiDemo.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %> - My ASP.NET Application</title>
    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>
    <webopt:bundlereference runat="server" path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="respond" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" runat="server" href="~/">
                        <img runat="server" src="~/Images/ctti_banner.jpg" style="width:300px" />
                    </a>
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li><a runat="server" href="~/">Home</a></li>
                        <li><a runat="server" href="~/About">About</a></li>
                        <li><a runat="server" href="~/Contact">Contact</a></li>
                        <li><a runat="server" href="~/Instructors">Instructors</a></li>
                        <li><a runat="server" href="~/Courses">Courses</a></li>
                        <li><a runat="server" href="~/Admin">Admin</a></li>                        
                        <li class="dropdown">
                            <a runat="server" href="#" data-toggle="dropdown" class="dropdown-toggle">
                                Students<b class="caret"></b>
                            </a>
                            <ul class="dropdown-menu">
                                <li><a runat="server" href="~/Students/Enrollment">Enrollment</a></li>
                                <li><a runat="server" href="~/Students/Payment">Payment</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
                <div>
                    <asp:Label ID="uxWelcome" runat="server" ForeColor="White"></asp:Label>
                      <asp:HyperLink ID="uxLogin" runat="server"></asp:HyperLink>
                </div>
            </div>
        </div>
        <div class="container body-content">
            <div class="row">
                <div class="col-md-1">
                     
                </div>
                <div class="col-md-10" style="height:300px">
                    <asp:ContentPlaceHolder ID="MainContent" runat="server">
                    </asp:ContentPlaceHolder>             
                </div>
                <div class="col-md-1">
                                 
                </div>
            </div>            
            <hr />
            <footer>
                <p>© <%: DateTime.Now.Year %> - My ASP.NET Application</p>
            </footer>
        </div>
    </form>
</body>
</html>
//logout
//site.master.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Page.User.Identity.IsAuthenticated)
{
uxWelcome.Text = "Welcome " + Page.User.Identity.Name;
uxLogin.Text = "Logout";
}
else
{
uxWelcome.Text = string.Empty;
uxLogin.Text = "LogIn";
uxLogin.PostBackUrl = "~/Login.aspx";
}
}
//linked button
protected void uxLogin_Click(object sender, EventArgs e)
{
if (Page.User.Identity.IsAuthenticated)
{
//remove the authentication
uxWelcome.Text = string.Empty;
uxLogin.Text = "LogIn";
uxLogin.PostBackUrl = "~/Login.aspx";
FormsAuthentication.SignOut();
Session.Clear();
Session.Abandon();
Response.Redirect("~/Default.aspx");
}
}


 
No comments:
Post a Comment