/// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
/// ' Microsoft Content Management Server - Sample Code 
/// '
/// ' This sample code is provided "AS IS" with no warranties, and confers no rights. 
/// ' You assume all risk for your use.  2001 Microsoft Corporation. All rights reserved.
/// '
/// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

using System;
using System.Web;
using System.Web.SessionState;
using System.Security;
using Microsoft.ContentManagement.Publishing;
using Microsoft.ContentManagement.Web;
using Microsoft.ContentManagement.Web.Security;
using Microsoft.ContentManagement.Web.Caching;
//Add reference for localization
using System.EnterpriseServices;
using System.Globalization;
using System.Resources;
using System.Threading;

namespace WoodgroveNet.Templates
{
	/// <summary>
	/// Summary description for ManualLogin.
	/// </summary>
	public class ManualLogin : System.Web.UI.Page
	{

		protected System.Web.UI.WebControls.CheckBox CheckPersistent;
		protected System.Web.UI.HtmlControls.HtmlGenericControl OutMessage;
		protected System.Web.UI.HtmlControls.HtmlGenericControl SSLWarning;
		protected System.Web.UI.HtmlControls.HtmlInputText TxtInsDomains;
		protected System.Web.UI.HtmlControls.HtmlInputText TxtNTUser;
		protected System.Web.UI.HtmlControls.HtmlInputText TxtNTPassword;
		protected System.Web.UI.WebControls.Literal LTManualLoginNTUserName;
		protected System.Web.UI.WebControls.Literal LTManualLoginDomain;
		protected System.Web.UI.WebControls.Literal LTManualLoginNTPassword;
		protected System.Web.UI.WebControls.Literal LTManualLoginRememberCredentials;

		protected string LTInvalidUserMessage;
		protected string LTSSLWarningMessage;
		protected string LTSSLStatus;
		protected string LTAccessDeniedMessage;
		protected string LTGeneralExceptionMessage;
		protected string LTArgumentOutOfRangeExceptionMessage;
//		
		//Resource Manager for localization
		protected ResourceManager LocalizationResourceManager;
		protected System.Web.UI.HtmlControls.HtmlInputButton ManualLoginSubmit;

		
		#region CMS Placeholders
		protected Microsoft.ContentManagement.WebControls.SingleImagePlaceholderControl Singleimageplaceholdercontrol4;
		#endregion CMS Placeholders
		
		private void Page_Load(object sender, System.EventArgs e)
		{
			string CheckSSLStatus = String.Empty;

			//Picking Up the Literals from the Resource Files
			//If you want to display the list of supported domains on the browser, uncomment this line of cide
			//LTManualLoginSupportedDomains.Text = LocalizationResourceManager.GetString("LoginSupportedDomains");
			LTManualLoginDomain.Text = LocalizationResourceManager.GetString("LoginDomain");
			LTManualLoginNTUserName.Text = LocalizationResourceManager.GetString("LoginNTUserName");
			LTManualLoginNTPassword.Text = LocalizationResourceManager.GetString("LoginNTPassword");
			LTManualLoginRememberCredentials.Text = LocalizationResourceManager.GetString("LoginRememberCredentials");
			ManualLoginSubmit.Value = LocalizationResourceManager.GetString("LoginButton");
			LTInvalidUserMessage= LocalizationResourceManager.GetString("InvalidUserMessage");
			LTAccessDeniedMessage = LocalizationResourceManager.GetString("AccessDeniedMessage");
			LTGeneralExceptionMessage = LocalizationResourceManager.GetString("GeneralExceptionMessage");
			LTArgumentOutOfRangeExceptionMessage = LocalizationResourceManager.GetString("ArgumentOutOfRangeExceptionMessage");
			LTSSLStatus = LocalizationResourceManager.GetString("LoginHTTPS");
			LTSSLWarningMessage = LocalizationResourceManager.GetString("LoginSSLWarning");

			//Code for Tracing Functionality
			string TracingSeperator = " :: ";
			// Write a line to the Listener
			System.Diagnostics.Trace.WriteLine(System.DateTime.Now + TracingSeperator + "Template" + TracingSeperator + "ManualLogin" + TracingSeperator + "This is the Page Load for the ManualLogin Template.");
			// Record your output
			System.Diagnostics.Trace.Flush();
			//End of Code for Tracing Functionality

			//Check if SSL is on. If not, display an error message on the browser.
			CheckSSLStatus = Request.ServerVariables[LTSSLStatus].ToString();
			if (!(CheckSSLStatus == "on"))
			{
				SSLWarning.InnerHtml = "<FONT COLOR=red><B>" + LTSSLWarningMessage + "</B></FONT>";
			}
		}

		#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);
			LocalizationResourceManager = (ResourceManager) Application["WoodgroveNetResourceManager"];

		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ManualLoginSubmit.ServerClick += new System.EventHandler(this.ManualLoginSubmit_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ManualLoginSubmit_ServerClick(object sender, System.EventArgs e)
		{	
			//This is the code that is implemented when Manual
			//Login Button is clicked on the Login Form
			string NTDomain = TxtInsDomains.Value;
			string NTUser = TxtNTUser.Value;
			string NTPassword = TxtNTPassword.Value;
			bool persist = CheckPersistent.Checked;
				
			NTDomain = NTDomain.Trim();
			//Uncomment this code if you are displaying the list of domain names on the browser
			/*if(NTDomain.Equals(""))
			{
				//If it got here, it means the domain entered is invalid.
				OutMessage.InnerHtml = "<FONT COLOR=yellow>" + LTInvalidUserMessage + "</FONT>";
			}
			else
			{
			*/
				try
				{
					//Use this if conditioin if you are displaying the list of domain names on the browser
					/*if ( NTDomain.Substring(0, 5).ToUpper().Equals("WINNT") && NTUser.IndexOf( "@" ) == -1 )
					{
						NTUser = FormWinNTLoginName( NTDomain, NTUser );
					}*/
					if(NTUser.IndexOf( "@" ) == -1)
					{
						//Use this function call if you are displaying the list of domain names on the browser
						//NTUser = FormWinNTLoginName("WinNT", NTDomain, NTUser);
						NTUser = FormWinNTLoginName("WinNT://", NTDomain, NTUser);
					}
				}
				catch(ArgumentOutOfRangeException)
				{
					OutMessage.InnerHtml = "<tr><td colspan=3><FONT COLOR=yellow>" + LTArgumentOutOfRangeExceptionMessage + "</FONT></td></tr>";
				}
				try
				{
					CmsAuthenticationTicket ticket = CmsFormsAuthentication.AuthenticateAsUser(NTUser, NTPassword);
					
					if( ticket != null )
					{
						CmsFormsAuthentication.RedirectFromLoginPage(ticket, true, CheckPersistent.Checked); 

					}
		
					    
					//If it got here, it means the redirect didn't occur so the username/password is not valid.
					OutMessage.InnerHtml = "<FONT COLOR=yellow>" + LTInvalidUserMessage + "</FONT>";
				}
				
				//Catching the Exceptions thrown
				//catch(CmsAccessDeniedException)
				//{
				//	OutMessage.InnerHtml = "<FONT COLOR=yellow>" + LTAccessDeniedMessage + "</FONT>";
				//}
				catch(Exception)
				{
					OutMessage.InnerHtml = "<FONT COLOR=yellow>" + LTGeneralExceptionMessage + "</FONT>";
				}
									
			//Uncomment this if you are displaying the list of domain names on the browser
			//}
		}//End of Button Click
	
		//Other Functions
		//Use this function if you are displaying the list of domain names on the browser
		/*
		string FormWinNTLoginName( string domain, string user )
		{
			return domain + "/" + user;
		}
		*/
		string FormWinNTLoginName( string domaintype, string domain, string user )
		{
			return domaintype + domain + "/" + user;
		}
	}
}
