C# 维护稳定用户会话的标准方法是什么?

C# 维护稳定用户会话的标准方法是什么?,c#,asp.net,iis,C#,Asp.net,Iis,在ASP.NET中维护稳定用户会话的标准方法是什么,而不必担心IIS回收、断开连接的用户、重新打开浏览器等等我希望我们的用户不必登录,除非每月登录一次,无论发生什么情况 如果我使用自己的登录控件(不是标准的asp.net登录控件),这也很重要吗?如果我理解正确,我想我需要手动创建身份验证票据。您可以在用户的机器上设置身份验证cookie,并将其设置为在月底过期。然后,您的身份验证代码可以简单地检查是否存在cookie,并根据cookie的内容自动登录用户。您可以在用户的机器上设置身份验证cook

在ASP.NET中维护稳定用户会话的标准方法是什么,而不必担心IIS回收、断开连接的用户、重新打开浏览器等等我希望我们的用户不必登录,除非每月登录一次,无论发生什么情况


如果我使用自己的登录控件(不是标准的asp.net登录控件),这也很重要吗?如果我理解正确,我想我需要手动创建身份验证票据。

您可以在用户的机器上设置身份验证cookie,并将其设置为在月底过期。然后,您的身份验证代码可以简单地检查是否存在cookie,并根据cookie的内容自动登录用户。

您可以在用户的机器上设置身份验证cookie,并将其设置为在月底过期。然后,您的身份验证代码可以简单地检查是否存在cookie,并根据cookie的内容自动登录用户。

我不知道是否有“标准”方法。这将取决于你的申请。基本的变化是,您将会话数据转换为某种形式,这种形式在回收过程中保持不变。我有一个应用程序,其中保存了有关“旅行”的信息,并将其序列化为xml文件。下面是我在这种情况下使用的代码。它可能是一个有用的凝视场所:

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Cravens.Infrastructure.Logging;

namespace TruckTrackerWeb.Code
{
    public class TripSessions
    {
        private const string _relativeSesionFile = "~/App_Data/TripSessions.txt";
        private readonly string _sessionFile;
        private readonly ILogger _logger;
        private readonly Dictionary<Guid, TripSession> _sessions;
        private readonly TimeSpan _maxAge = new TimeSpan(1, 0, 0, 0);

        public TripSessions(ILogger logger, HttpContextBase httpContextBase)
        {
            _logger = logger;


            _sessionFile = httpContextBase.Server.MapPath(_relativeSesionFile);

            _sessions = ReadSessionFile();
        }

        public TripSession CreateSession(string userName, int truckId)
        {
            try
            {
                TripSession tripSession = new TripSession
                                              {
                                                  Id = Guid.NewGuid(),
                                                  Expiration = DateTime.Now + _maxAge,
                                                  TruckId = truckId,
                                                  UserName = userName
                                              };
                _sessions[tripSession.Id] = tripSession;
                SaveSessionFile();
                _logger.Debug("Created session for: username=" + userName + ",truckid=" + truckId);
                return tripSession;
            }
            catch (Exception ex)
            {
                _logger.Error("Failed to create session. ", ex);
            }
            return null;
        }

        public TripSession GetSession(Guid id)
        {
            if(_sessions.ContainsKey(id))
            {
                return _sessions[id];
            }
            return null;
        }

        private void SaveSessionFile()
        {
            _logger.Debug("Saving trip session data to file.");
            List<string> lines = new List<string>();
            foreach (KeyValuePair<Guid, TripSession> keyValuePair in _sessions)
            {
                TripSession tripSession = keyValuePair.Value;
                lines.Add(tripSession.ToString());
            }
            File.WriteAllLines(_sessionFile, lines.ToArray());
        }

        private Dictionary<Guid, TripSession> ReadSessionFile()
        {
            _logger.Debug("******READING TRIP SESSION FILE**********");
            Dictionary<Guid, TripSession> result = new Dictionary<Guid, TripSession>();

            if(!File.Exists(_sessionFile))
            {
                _logger.Debug("The session file does not exist. file=" + _sessionFile);
                return result;
            }

            string[] lines = File.ReadAllLines(_sessionFile);
            foreach (string line in lines)
            {
                TripSession tripSession = TripSession.ParseLine(line);
                if(tripSession!=null && (DateTime.Now - tripSession.Expiration)<_maxAge)
                {
                    result[tripSession.Id] = tripSession;
                    _logger.Debug("ADDED---->" + line);
                }
                else
                {
                    _logger.Debug("EXPIRED-->" + line);
                }
            }
            return result;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Web;
使用crawens.Infrastructure.Logging;
命名空间TruckTrackerWeb.Code
{
公开课
{
private const string\u relativesessionfile=“~/App\u Data/TripSessions.txt”;
私有只读字符串_sessionFile;
专用只读ILogger\u记录器;
专用只读字典会话;
私有只读时间跨度_maxAge=新时间跨度(1,0,0,0);
公共TripSessions(ILogger记录器、HttpContextBase HttpContextBase)
{
_记录器=记录器;
_sessionFile=httpContextBase.Server.MapPath(_relativesessionfile);
_sessions=ReadSessionFile();
}
公共TripSession CreateSession(字符串用户名,int-truckId)
{
尝试
{
TripSession TripSession=新TripSession
{
Id=Guid.NewGuid(),
Expiration=DateTime.Now+\u maxAge,
卡车司机,
用户名=用户名
};
_会话[tripSession.Id]=tripSession;
SaveSessionFile();
_logger.Debug(“为以下对象创建会话:username=“+username+”,truckid=“+truckid”);
返回会话;
}
捕获(例外情况除外)
{
_logger.Error(“创建会话失败”,例如);
}
返回null;
}
公共TripSession GetSession(Guid id)
{
if(_sessions.ContainsKey(id))
{
返回会话[id];
}
返回null;
}
私有void SaveSessionFile()
{
_Debug(“将跳闸会话数据保存到文件中”);
列表行=新列表();
foreach(KeyValuePair KeyValuePair in_会话)
{
TripSession TripSession=keyValuePair.Value;
Add(tripSession.ToString());
}
File.writeAllines(_sessionFile,lines.ToArray());
}
专用词典ReadSessionFile()
{
_logger.Debug(“******读取跳闸会话文件**********”);
字典结果=新字典();
如果(!File.Exists(_sessionFile))
{
_logger.Debug(“会话文件不存在。文件=“+\u sessionFile”);
返回结果;
}
string[]lines=File.ReadAllLines(_sessionFile);
foreach(行中的字符串行)
{
TripSession TripSession=TripSession.ParseLine(行);

if(tripSession!=null&&(DateTime.Now-tripSession.Expiration)我不知道是否有“标准”方法。这取决于您的应用程序。基本的变化是,您将会话数据转换为某种形式,并在回收过程中保持。我有一个应用程序,我在其中保存有关“旅行”的信息,并将其序列化为xml文件。下面是我在这种情况下使用的代码。这可能是一个有用的示例ul起航地点:

using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using Cravens.Infrastructure.Logging;

namespace TruckTrackerWeb.Code
{
    public class TripSessions
    {
        private const string _relativeSesionFile = "~/App_Data/TripSessions.txt";
        private readonly string _sessionFile;
        private readonly ILogger _logger;
        private readonly Dictionary<Guid, TripSession> _sessions;
        private readonly TimeSpan _maxAge = new TimeSpan(1, 0, 0, 0);

        public TripSessions(ILogger logger, HttpContextBase httpContextBase)
        {
            _logger = logger;


            _sessionFile = httpContextBase.Server.MapPath(_relativeSesionFile);

            _sessions = ReadSessionFile();
        }

        public TripSession CreateSession(string userName, int truckId)
        {
            try
            {
                TripSession tripSession = new TripSession
                                              {
                                                  Id = Guid.NewGuid(),
                                                  Expiration = DateTime.Now + _maxAge,
                                                  TruckId = truckId,
                                                  UserName = userName
                                              };
                _sessions[tripSession.Id] = tripSession;
                SaveSessionFile();
                _logger.Debug("Created session for: username=" + userName + ",truckid=" + truckId);
                return tripSession;
            }
            catch (Exception ex)
            {
                _logger.Error("Failed to create session. ", ex);
            }
            return null;
        }

        public TripSession GetSession(Guid id)
        {
            if(_sessions.ContainsKey(id))
            {
                return _sessions[id];
            }
            return null;
        }

        private void SaveSessionFile()
        {
            _logger.Debug("Saving trip session data to file.");
            List<string> lines = new List<string>();
            foreach (KeyValuePair<Guid, TripSession> keyValuePair in _sessions)
            {
                TripSession tripSession = keyValuePair.Value;
                lines.Add(tripSession.ToString());
            }
            File.WriteAllLines(_sessionFile, lines.ToArray());
        }

        private Dictionary<Guid, TripSession> ReadSessionFile()
        {
            _logger.Debug("******READING TRIP SESSION FILE**********");
            Dictionary<Guid, TripSession> result = new Dictionary<Guid, TripSession>();

            if(!File.Exists(_sessionFile))
            {
                _logger.Debug("The session file does not exist. file=" + _sessionFile);
                return result;
            }

            string[] lines = File.ReadAllLines(_sessionFile);
            foreach (string line in lines)
            {
                TripSession tripSession = TripSession.ParseLine(line);
                if(tripSession!=null && (DateTime.Now - tripSession.Expiration)<_maxAge)
                {
                    result[tripSession.Id] = tripSession;
                    _logger.Debug("ADDED---->" + line);
                }
                else
                {
                    _logger.Debug("EXPIRED-->" + line);
                }
            }
            return result;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Web;
使用crawens.Infrastructure.Logging;
命名空间TruckTrackerWeb.Code
{
公开课
{
private const string\u relativesessionfile=“~/App\u Data/TripSessions.txt”;
私有只读字符串_sessionFile;
专用只读ILogger\u记录器;
专用只读字典会话;
私有只读时间跨度_maxAge=新时间跨度(1,0,0,0);
公共TripSessions(ILogger记录器、HttpContextBase HttpContextBase)
{
_记录器=记录器;
_sessionFile=httpContextBase.Server.MapPath(_relativesessionfile);
_sessions=ReadSessionFile();
}
公共TripSession CreateSession(字符串用户名,int-truckId)
{
尝试
{
TripSession TripSession=新TripSession
{
Id=Guid.NewGuid(),
Expiration=DateTime.Now+\u maxAge,
卡车司机,
用户名=用户名