0

単一のユーザー名から複数のログインを防ぐ方法は? ユーザー名とパスワードをデータベースに保存しています。ユーザーが一度に 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:&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>

このエラーが発生しています:

タイプ「SingleSessionEnforcement」を読み込めませんでした。
説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

例外の詳細: System.Web.HttpException: タイプ 'SingleSessionEnforcement' を読み込めませんでした。

エラーの添付画像を参照してください。

エラー画像

問題を解決するために私を導いてください。

4

2 に答える 2

1

type 属性には名前空間を含める必要があります。

<add name="SingleSessionEnforcement" type="Demo1.SingleSessionEnforcement" />
于 2016-04-01T14:25:20.647 に答える