locked
Creating and using before/after DateTime condition for TIME RRS feed

  • Question

  • User1771308999 posted

    Trying to restrict user's access to the website based on time range, i.e.: 8am to 5pm

    While one alternative is using server job via IIS, I unfortunate do not have direct access to IIS. Thus, hoping to find a code that may the trick.

    I tried:

        var currDate = DateTime.Now.ToShortDateString();
        var currTime = DateTime.Now.ToShortTimeString();
    
        DateTime currSystemTime = DateTime.Parse(currTime);
        DateTime morningDeadline = DateTime.Parse("7:29 AM");
        DateTime eveningDeadline = DateTime.Parse("5:01 PM");
    
        if(currSystemTime >= morningDeadline && currSystemTime >= eveningDeadline && Roles.IsUserInRole("ratingTrn")){
            //Deny access
        }

    But the code isn't consistent. Sometime evening deadline would work but then not the morning deadline. So any assistant would be greatly appreciated! Many thanks in advance!

    Wednesday, August 20, 2014 8:54 AM

Answers

  • User-821857111 posted

    Not sure why you are messing around converting dates to strings and back again... but:

    if(DateTime.Now.Hour >= 8 && DateTime.Now.Hour <= 16 && Roles.IsUserInRole("ratingTrn")){
        //Deny access
    } 


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 20, 2014 9:10 AM

All replies

  • User-1423995609 posted

    Shouldn't it be after 8AM and before 5PM ? Your condition says

    if(currSystemTime >= morningDeadline && currSystemTime >= eveningDeadline && Roles.IsUserInRole("ratingTrn")){
            //Deny access
        }

    currSystemTime >= morningDeadline (after 8AM) && currSystemTime >= eveningDeadline (after 5PM), should be currSystemTime <= eveningDeadline

    if(currSystemTime >= morningDeadline && currSystemTime <= eveningDeadline && Roles.IsUserInRole("ratingTrn")){
            //Deny access
        }

    Wednesday, August 20, 2014 9:08 AM
  • User-821857111 posted

    Not sure why you are messing around converting dates to strings and back again... but:

    if(DateTime.Now.Hour >= 8 && DateTime.Now.Hour <= 16 && Roles.IsUserInRole("ratingTrn")){
        //Deny access
    } 


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 20, 2014 9:10 AM
  • User379720387 posted

    Let me throw another complication in the mix.

    If you are dealing with different time zones, you will need to account for those.

    And, if you are testing locally, any datetime.now entry will be computer time, while the same entry made on the live site will be UTC.

    Wednesday, August 20, 2014 11:25 AM
  • User1771308999 posted

    _Manvel_ -- you're correct. I previously had it as:

    if(currSystemTime >= morningDeadline && currSystemTime <= eveningDeadline && Roles.IsUserInRole("ratingTrn")){
            //Deny access
        }

    But by this morning, found it did not deny access as it was still before 8AM. So that didn't work for me which was why I tried tweaking it and then copy n paste as is..

    Wednesday, August 20, 2014 11:53 AM
  • User1771308999 posted

    Mikesdotnetting - I couldn't find a concrete answer -- so found that via this post: http://forums.asp.net/t/1025431.aspx?How+to+compare+Time+in+C+

    Sure enough, there wasn't an answer marked so I figured I'd give it a shot..

    Wednesday, August 20, 2014 11:58 AM
  • User1771308999 posted

    Wavemaster --

    Good point, without access to IIS to use service job -- what do you suggest?? Mikesdotnetting's code seems to work and so when each user use the portal, it will check their local computer time. Thus the only way to bypass it is if they change their computer time. 

    How could I reply solely on server time and take into account each timezone ??

    Wednesday, August 20, 2014 12:02 PM
  • User379720387 posted

    You will need to get a time zone for each user.

    var query3 = @"SELECT tzDescription FROM TimeZones tz 
            JOIN UserProfile up ON up.tzId = tz.tzId 
            WHERE up.UserId = @0";
        var timezone = db.QueryValue(query3, theuser);
        TimeZoneInfo userTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timezone);

    in your webgrid:

    grid.Column("ttInStamp", "Date", format: @<text>@TimeZoneInfo.ConvertTimeFromUtc(item.ttInStamp, userTimeZone).ToString("d")</text>),

    TimeZoneInfo does not need any extra dependencies.

    Wednesday, August 20, 2014 1:05 PM
  • User-821857111 posted

    so when each user use the portal, it will check their local computer time

    No it won't. The code executing on the server is subject to the time on the server. 

    If you are working with people in different time zones, you need to store their time zone as part of their profile. Then you can use that to work out what time it is with them relative to the server time.

    Wednesday, August 20, 2014 4:09 PM
  • User1771308999 posted

    Mikesdotnetting - 

    Gotcha. Thank you!

    Friday, August 22, 2014 9:49 AM