0

ASP.NET で最初の手順を実行しましたが、クラスに問題があります。セッションをサポートし、Web サイトのユーザー数に関する情報を収集するために、新しいカスタム クラスを作成したいと考えています。ファイル MySession.cs にクラスを作成しました。このクラスは、WebMatrix によって「App_data」というディレクトリに配置されています。このクラスを .cshtml ファイルで使用しようとすると、そのクラスが見つからないという情報がスローされます。私は Web で見つけました。クラスは App_Code に配置する必要があるため、実行しました。ただし、現時点では、「Request」などのクラスが見つからないというエラーが表示されます。

WebMatrix でカスタム クラスを使用するには?

.cshtml ファイルからの私の c# コードは次のようになります。

@{  
  MySession session = new MySession(60);
  session.start();
  var db = Database.Open("studia");
  var data = db.Query("SELECT COUNT(*) as total FROM sessions");
}

クラスファイルは次のようになります。

using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
using System.Data;
using System.Data.SqlClient;

    /// <summary>
    /// Summary description for sesss
    /// </summary>
    public class MySession
    {
        private String _session_id;
        private int _session_time;

        public MySession(int session_time = 60)
        {
            _session_time = session_time;
            using (MD5 md5Hash = MD5.Create())
            {
                _session_id = (Request.Cookies["session_id"] != null) ? Server.HtmlEncode(Request.Cookies["sesion_id"].Value) : GetMd5Hash(md5Hash, DateTime.Now);
            }
            cleanup();
        }

        public bool isLogged()
        {
            if (Request.Cookies["session_id"] != null)
            {
                return true;
            }
            return false;
        }

        public void start(string username)
        {
            DateTime now = DateTime.Now;

            var db = Database.Open("studia");

            if (isLogged())
            {
                db.Query("UPDATE sessions SET start_time = " + now + " WHERE session_id = " + _session_id);
            }
            else
            {
                db.Query("INSERT INTO sessions (id, start_time, username) VALUES ('" + _session_id + "', '" + now + "', '" + username + "'");
            }

            HttpCookie session_cookie = new HttpCookie("session_id");
            session_cookie.Value = DateTime.Now;
            session_cookie.Expires = DateTime.Now.AddSeconds(_session_time);
            Response.Cookies.Add(aCookie);
        }

        public void cleanup()
        {
            var db = Database.Open("studia");
            db.Query("DELETE FROM sessions WHERE start_time < " + (DateTime.Now - _session_time));
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        // Verify a hash against a string.
        static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input.
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
4

1 に答える 1

1

(ページ ファイル内からではなく) クラス内の Request オブジェクトを参照する場合は、 HttpContext.Currentなどを使用する必要があります。

public bool isLogged()
{
  return HttpContext.Current.Request.Cookies["session_id"] != null;
}
于 2012-10-13T09:07:11.820 に答える