这有很多需要查看,但应该给你一个很好的起点:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultipleUsers.aspx.cs" Inherits="WebApplicationCS2.MultipleUsers" %>
<!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:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<div style="float:left; border:1px solid gray; min-height:200px; width:40%;">
<asp:Timer runat="server" ID="tmrUsersList" Interval="1000"></asp:Timer>
<asp:UpdatePanel runat="server" ID="pnlAjaxUserList">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tmrUsersList" />
</Triggers>
<ContentTemplate>
<table style="width:100%;">
<asp:Repeater runat="server" ID="rptUsersList">
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="lblUserName" Text='<%# Eval("UserName")%>' />
</td>
<td>
<asp:LinkButton runat="server" ID="lnkChallenge" Enabled='<%# Eval("CanChallenge")%>'
Text="Challenge" CommandArgument='<%# Eval("UserName")%>'
OnCommand="lnkChallenge_Command" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div style="float:left;">
Choose username:
<asp:TextBox runat="server" ID="txtChosenName"></asp:TextBox>
<asp:Button runat="server" ID="btnSignin" Text="Sign In"
OnClick="btnSignin_Click" />
<br />
<asp:Label runat="server" ID="lblMessage" ForeColor="Red" Visible="false" EnableViewState="false"></asp:Label>
</div>
</div>
</form>
</body>
</html>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplicationCS2
{
public partial class MultipleUsers : System.Web.UI.Page
{
private const string UserNameKey = "SitePlayer_UserName";
private string CurrentUserName;
private List<GameUser> lGameUsers;
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie playerCookie = Request.Cookies[UserNameKey];
CurrentUserName = (playerCookie != null ? playerCookie.Value : null);
if (CurrentUserName != null)
{
// Update cache to indicate user is still online.
Cache.Add(UserNameKey + CurrentUserName, CurrentUserName, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(1), System.Web.Caching.CacheItemPriority.Normal, null);
}
lGameUsers = GetUserList();
rptUsersList.DataSource = lGameUsers;
rptUsersList.DataBind();
}
protected void btnSignin_Click(object sender, EventArgs e)
{
string chosenName = txtChosenName.Text.Trim();
foreach (GameUser u in lGameUsers)
{
if (string.Compare(chosenName, u.UserName, StringComparison.OrdinalIgnoreCase) == 0)
{
lblMessage.Text = "Username already in use.";
lblMessage.Visible = true;
return;
}
}
Cache.Add(UserNameKey + chosenName, chosenName, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(1), System.Web.Caching.CacheItemPriority.Normal, null);
Response.AppendCookie(new HttpCookie(UserNameKey, chosenName));
}
protected void lnkChallenge_Command(object sender, CommandEventArgs e)
{
}
class GameUser
{
public string UserName { get; set; }
public bool CanChallenge { get; set; }
}
private List<GameUser> GetUserList()
{
List<GameUser> userList = new List<GameUser>();
foreach (System.Collections.DictionaryEntry cacheItem in Cache)
{
if (cacheItem.Key.ToString().StartsWith(UserNameKey))
{
string name = cacheItem.Value.ToString();
if (string.Compare(CurrentUserName, name, StringComparison.OrdinalIgnoreCase) != 0)
{
GameUser u = new GameUser()
{
UserName = name,
CanChallenge = (CurrentUserName != null)
};
userList.Add(u);
}
}
}
return userList;
}
}
}
要查看运行中的代码,请运行网站,浏览到表单,然后使用两种不同的浏览器登录。
您需要执行的步骤包括验证用户仅登录一次、连接“挑战”链接按钮等。