Just to share a simple solution in creating an account logout management for a web application:
<h1>
User Lockout Management</h1>
<table width="400" bgcolor="#A8CBFF">
<tr>
<td class="style4">
Login Name:</td>
<td bgcolor="#E4E4E4"><asp:Label runat="server" ID="UserNameLabel" Font-Bold="True"
Font-Names="Verdana" ForeColor="Blue" Font-Size="9pt"
style="color: #808080; font-size: medium;"></asp:Label></td>
</tr>
<tr>
<td class="style4">
Approved:</td>
<td bgcolor="#E4E4E4">
<asp:CheckBox ID="IsApproved" runat="server" AutoPostBack="true"
ForeColor="Blue" Font-Names="Verdana" Font-Size="9pt"
style="color: #333333; font-size: medium;"
oncheckedchanged="IsApproved_CheckedChanged" />
<asp:Label ID="StatusMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style4">
Locked Out:</td>
<td bgcolor="#E4E4E4">
<br />
<asp:Label runat="server" ID="LastLockoutDateLabel" ForeColor="Red" Font-Names="Verdana" Font-Size="9pt"></asp:Label>
<br />
<br />
<asp:Button runat="server" ID="UnlockUserButton" Text="Unlock User"
Font-Names="Verdana" Font-Size="9pt"
style="font-size: small; font-family: Tahoma"
onclick="UnlockUserButton_Click" BackColor="#666666" BorderColor="#666666"
BorderStyle="Solid" ForeColor="White" />
</td>
</tr>
</table>
Code-Behind
using System;
using System.Web.Security;
public partial class webAdmin_userLockoutManagement : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// If querystring value is missing, send the user to ManageUsers.aspx
string userName = Request.QueryString["user"];
if (string.IsNullOrEmpty(userName))
{
Response.Redirect("loginAccountList.aspx");
}
// Get information about this user
MembershipUser usr = Membership.GetUser(userName);
if (usr == null)
{
Response.Redirect("loginAccountList.aspx");
}
UserNameLabel.Text = usr.UserName;
IsApproved.Checked = usr.IsApproved;
if (usr.LastLockoutDate.Year < 2000)
{
LastLockoutDateLabel.Text = string.Empty;
}
else
{
LastLockoutDateLabel.Text = usr.LastLockoutDate.ToShortDateString();
UnlockUserButton.Enabled = usr.IsLockedOut;
}
}
}
protected void IsApproved_CheckedChanged(object sender, EventArgs e)
{
//Toggle the user's approved status
string userName = Request.QueryString["user"];
MembershipUser usr = Membership.GetUser(userName);
usr.IsApproved = IsApproved.Checked;
Membership.UpdateUser(usr);
StatusMessage.Text = "The user's approved status has been updated.";
}
protected void UnlockUserButton_Click(object sender, EventArgs e)
{
//Unlock the user account
string userName = Request.QueryString["user"];
MembershipUser usr = Membership.GetUser(userName);
usr.UnlockUser();
UnlockUserButton.Enabled = false;
StatusMessage.Text = "The user account has been unlocked.";
}
}
No comments:
Post a Comment