Page Title Вход Language MasterPageFile Site Master AutoEventWireup tr

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<%@ Page Title="Вход" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Logon.aspx.cs" Inherits="FormsAuthAd.Logon" %>
<%@ Import Namespace="FormsAuth" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<hgroup class="title">
<h1><%: Title %>.</h1>
</hgroup>
<section id="Logon">
<h2>Используйте для входа учетную запись.</h2>
<asp:Label ID="Label1" runat="server">Domain:</asp:Label><br />
<asp:TextBox ID="txtDomain" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server">Username:</asp:Label><br />
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server">Password:</asp:Label><br />
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br />
<asp:Button ID="btnLogin" runat="server" Text="Вход" OnClick="Login_Click"></asp:Button><br />
<asp:Label ID="errorLabel" runat="server" ForeColor="#ff3300"></asp:Label><br />
<asp:CheckBox ID="chkPersist" runat="server" Text="Сохранить Cookie" />
<script runat="server">
void Login_Click(object sender, EventArgs e)
{
string adPath = "LDAP://DC=step-group,DC=local";
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
if (true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
{
string groups = adAuth.GetGroups();
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
if (true == isCookiePersistent)
authCookie.Expires = authTicket.Expiration;
Response.Cookies.Add(authCookie);
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
}
else
{
errorLabel.Text = "Authentication did not succeed. Check user name and password.";
}
}
catch (Exception ex)
{
errorLabel.Text = "Error authenticating. " + ex.Message;
}
}
</script>
</section>
</asp:Content>