これは見直すことがたくさんありますが、非常に良い出発点を提供するはずです:
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;
}
}
}
コードの動作を確認するには、Web サイトを実行し、フォームを参照して、2 つの異なるブラウザーを使用してサインインします。
あなたがしなければならない手順は、ユーザーが 1 回だけサインインすることを検証すること、チャレンジ リンク ボタンを配線することなどです。