[英]How to prevent multiple user login from the same user id in asp.net using cache?
[英]How to prevent multiple login from single user name
如何防止单个用户名多次登录? 我正在将用户名和密码保存在数据库中。 我希望用户一次只能从1个地方登录。
有关如何配置ASP.NET应用程序的更多信息,请访问http://go.microsoft.com/fwlink/?LinkId=169433。
下面是我尝试过的代码
配置文件
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<httpModules>
<add name="SingleSessionEnforcement" type="SingleSessionEnforcement" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="false" >
<add name="SingleSessionEnforcement" type="SingleSessionEnforcement" />
</modules>
</system.webServer>
</configuration>
类文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace Demo1
{
public class SingleSessionEnforcement : IHttpModule
{
public SingleSessionEnforcement()
{
// No construction needed
}
private void OnPostAuthenticate(Object sender, EventArgs e)
{
Guid sessionToken;
HttpApplication httpApplication = (HttpApplication)sender;
HttpContext httpContext = httpApplication.Context;
// Check user's session token
if (httpContext.User.Identity.IsAuthenticated)
{
FormsAuthenticationTicket authenticationTicket =
((FormsIdentity)httpContext.User.Identity).Ticket;
if (authenticationTicket.UserData != "")
{
sessionToken = new Guid(authenticationTicket.UserData);
}
else
{
// No authentication ticket found so logout this user
// Should never hit this code
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
return;
}
MembershipUser currentUser = Membership.GetUser(authenticationTicket.Name);
// May want to add a conditional here so we only check
// if the user needs to be checked. For instance, your business
// rules for the application may state that users in the Admin
// role are allowed to have multiple sessions
Guid storedToken = new Guid(currentUser.Comment);
if (sessionToken != storedToken)
{
// Stored session does not match one in authentication
// ticket so logout the user
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
}
}
public void Dispose()
{
// Nothing to dispose
}
public void Init(HttpApplication context)
{
context.PostAuthenticateRequest += new EventHandler(OnPostAuthenticate);
}
}
}
ASPX.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo1
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//TextBox userNameTextBox = (TextBox)LoginUser.FindControl("UserName");
SingleSessionPreparation.CreateAndStoreSessionToken(userNameTextBox.Text);
}
}
}
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="Demo1.login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name: <asp:TextBox ID="userNameTextBox" runat="server"></asp:TextBox><br/>
Password: <asp:TextBox ID="PasswordTextBox" runat="server"></asp:TextBox><br/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
它给出了这个错误:
无法加载类型“ SingleSessionEnforcement”。
说明:执行当前Web请求期间发生未处理的异常。 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。异常详细信息:System.Web.HttpException:无法加载类型“ SingleSessionEnforcement”。
请参考错误的附件图片:
请指导我解决问题。
type属性需要包含名称空间:
<add name="SingleSessionEnforcement" type="Demo1.SingleSessionEnforcement" />
希望到这个时候您可以找到解决方案。 如果没有,请在下面查看我的评论:
问题出在web.config文件中。 Httpmodule在配置文件中只能启动一次。
如果是IIS 7.0或更低版本,请在system.web中使用以下版本, <httpModules> <add name="SingleSessionEnforcement" type="SingleSessionEnforcement" /> </httpModules>
如果它是> IIS 7.0,请在system.webServer中使用以下<modules> <add name="SingleSessionEnforcement" type="SingleSessionEnforcement" /> </modules>
, <modules> <add name="SingleSessionEnforcement" type="SingleSessionEnforcement" /> </modules>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.