2016-04-01 20 views
0

Tek kullanıcı adından çoklu oturum açma nasıl engellenir? Veritabanında kullanıcı adı ve şifre kaydediyorum. Kullanıcının sadece bir seferde 1 yerden giriş yapmasını istiyorum.Tek kullanıcı adından çoklu oturum açma nasıl engellenir?

ASP.NET uygulamanızı nasıl yapılandırılacağı hakkında daha fazla bilgi için lütfen Aşağıda

http://go.microsoft.com/fwlink/?LinkId=169433 Ben

Yapılandırma Dosyası

<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> 

Sınıf Dosyası

denedi bir koddur
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:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox ID="userNameTextBox" runat="server"></asp:TextBox><br/> 
      Password: <asp:TextBox ID="PasswordTextBox" runat="server"></asp:TextBox><br/> 
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
     </div> 
    </form> 
</body> 
</html> 
Bu hatayı veriyor

:

Error image

çözmek için bana yol edin:

Could not load type 'SingleSessionEnforcement'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Could not load type 'SingleSessionEnforcement'.

hatasının Ekteki resmi refere Lütfen sorun. Eğer çözüm var olabilirdi bu zamana kadar

<add name="SingleSessionEnforcement" type="Demo1.SingleSessionEnforcement" /> 
+2

CSS iyi olurdu sayfa düzeni için bir grup   'yerine. –

cevap

1

type özelliği, ad içermesi gerekir. Değilse, lütfen aşağıdaki yorumlarımı görünüz:

Sorun, web.config dosyasında. Httpmodule, yapılandırma dosyasında yalnızca bir kez başlatılabilir.

öyle> IIS 7.0 IIS 7.0 veya altında, System.Web aşağıdaki birini, <httpModules> <add name="SingleSessionEnforcement" type="SingleSessionEnforcement" /> </httpModules>

kullanırsanız, system.webServer aşağıdaki birini kullanın <modules> <add name="SingleSessionEnforcement" type="SingleSessionEnforcement" /> </modules>

0

Hope:

+0

Merhaba Siva, SO'ya hoş geldiniz! Ad alanının cevabında Richard'ın önerdiği tip niteliğine dahil edilmemesi gerekir mi? –

İlgili konular