小编And*_*eas的帖子

无法在ASP.NET中更新cookie

我为此疯狂.我可以写一个cookie,然后再读一遍.但在某些时候,我想更新它所拥有的价值.每当我再次获得cookie时,我会得到初始值,而不是更新的值.下面是我用于写入/更新和读取cookie的代码.

    private static HttpCookie WriteCookie(Guid siteId, string siteName)
    {
        var cookie = HttpContext.Current.Request.Cookies.Get("UserSettings");
        if(cookie != null) {
            cookie.Value = EncryptObject(new UserSettingsModel { SiteID = siteId, SiteName = siteName });
            HttpContext.Current.Response.Cookies.Set(cookie);
        }else {
            cookie = new HttpCookie("UserSettings") { Path = "/", Expires = DateTime.Now.AddDays(1), Value = EncryptObject(new UserSettingsModel { SiteID = siteId, SiteName = siteName }) };
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        return cookie;
    }

    public static UserSettingsModel GetUserSettings(string username = null)
    {
        var cookie = HttpContext.Current.Request.Cookies.Get("UserSettings");
        if (cookie == null || string.IsNullOrEmpty(cookie.Value))
        { …
Run Code Online (Sandbox Code Playgroud)

c# cookies model-view-controller asp.net-mvc

12
推荐指数
1
解决办法
2万
查看次数

在NTP和C#DateTime之间转换

我使用以下代码在NTP和C#DateTime之间进行转换.我认为前向反转是正确的,但倒退是错误的.

请参阅以下代码将8个字节转换为DatTime:

将NTP转换为DateTime

public static ulong GetMilliSeconds(byte[] ntpTime)
{
    ulong intpart = 0, fractpart = 0;

    for (var i = 0; i <= 3; i++)
        intpart = 256 * intpart + ntpTime[i];
    for (var i = 4; i <= 7; i++)
        fractpart = 256 * fractpart + ntpTime[i];

    var milliseconds = intpart * 1000 + ((fractpart * 1000) / 0x100000000L);

    Debug.WriteLine("intpart:      " + intpart);
    Debug.WriteLine("fractpart:    " + fractpart);
    Debug.WriteLine("milliseconds: " + milliseconds);
    return milliseconds;
}

public static DateTime ConvertToDateTime(byte[] ntpTime) …
Run Code Online (Sandbox Code Playgroud)

c# ntp

6
推荐指数
1
解决办法
3507
查看次数

请求锁屏访问会在mscorlib.dll中引发异常挂起或抛出异常

我使用以下方法在WinRT中请求锁屏访问:

public async void RequestLockScreenAccess()
    {
        var status = BackgroundExecutionManager.GetAccessStatus();
        if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.Denied)
            status = await BackgroundExecutionManager.RequestAccessAsync();
        switch (status)
        {
            case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
                _mainInfo.NotifyUser = "This app is on the lock screen and has access to Always-On Real Time Connectivity.";
                break;
            case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
                _mainInfo.NotifyUser = "This app is on the lock screen and has access to Active Real Time Connectivity.";
                break;
            case BackgroundAccessStatus.Denied:
                _mainInfo.NotifyUser = "This app is not on the lock screen.";
                break;
            case BackgroundAccessStatus.Unspecified: …
Run Code Online (Sandbox Code Playgroud)

c# windows-8 windows-runtime

4
推荐指数
1
解决办法
3225
查看次数