using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Orciid.Core;

namespace orciid.imgview
{
	/// <summary>
	/// Summary description for refreshtoken.
	/// </summary>
	public class refreshtoken : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			string tokenid;
			bool tokenisvalid = false;
			try 
			{
				tokenid = "IVtoken" + Request.QueryString.GetValues("t")[0].ToString();
				foreach(string item in Application.Contents) 
				{
					if (item == tokenid) 
					{
						if (DateTime.Compare(((Token)Application.Get(item)).timestamp, DateTime.Now) >= 0) 
						{
							tokenisvalid = true;
							Token token = (Token)Application.Get(item);
							token.timestamp = DateTime.Now + System.TimeSpan.FromMinutes(60);
							Application.Set(tokenid, token);
						}
						break;
					}
				}
				if (!tokenisvalid) 
				{
					Response.Status = "401 Unauthorized";
					Response.End();
				}
			} 
			catch 
			{
				Response.Status = "401 Unauthorized";
				Response.End();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
