using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Drawing;
using EncryptDecryptEngine;
using DataLibrary;
namespace CoreSavingLibrary
{
public class RestoreAuthentication : Page
{
///
/// form1 control.
///
///
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
///
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
///
/// LbMessage control.
///
///
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
///
protected global::System.Web.UI.WebControls.Label LbMessage;
protected void Page_Load(object sender, EventArgs e)
{
// กำหนดค่าเริ่มต้นเป็นค่าว่าง
LbMessage.Text = "";
LbMessage.ForeColor = Color.Red;
string tokenPassword = "";
int conIndex = 0;
// หาค่า virtual directory จากฟังก์ชัน เก็บไว้ในตัวแปร
string vDir = WebUtil.GetVirtualDirectory();
// ถ้า virtual directory เป็นค่าว่างให้แจ้ง error และหยุดการทำงาน
if (string.IsNullOrEmpty(vDir))
{
LbMessage.Text = "ไม่พบ virtual directory";
return;
}
// หาค่า GCOOP path
string gcoopPath = WebUtil.GetGcoopPath();
// ดึงค่า token password และ connection index จาก request ถ้าไม่สำเร็จให้แจ้ง error และจบการทำงาน
try
{
tokenPassword = Decryption.SessionFormat(Request["p"]);
if (string.IsNullOrEmpty(tokenPassword))
{
throw new Exception();
}
conIndex = int.Parse(Request["c"].Trim());
}
catch
{
LbMessage.Text = "เกิดข้อผิดผลาด เนื่องจากข้อมูลการเข้าใช้งานไม่สมบูรณ์";
return;
}
// ตั้งค่า cookie connection index ใหม่
Response.Cookies["cid"].Value = conIndex.ToString();
Response.Cookies["cid"].Expires = DateTime.Now.AddDays(7);
// ดึงค่า session id เก็บไว้ในตัวแปร
string sessionId = Session.SessionID;
// ประกาศตัวแปร transaction
Sta ta = null;
// ประกาศตัวแปร xmlconfig ถ้าเป็นค่า null ให้แจ้ง error และหยุดการทำงาน
XmlConfigService xml = null;
try
{
xml = new XmlConfigService(gcoopPath);
if (xml == null) throw new Exception();
}
catch
{
LbMessage.Text = "ไม่สามารถอ่านค่าคอนฟิกของระบบได้";
return;
}
// ลอง connect database ถ้าไม่สำเร็จให้แจ้ง error และหยุดการทำงาน
string connectionString = "";
try
{
connectionString = xml.ConnectionStringData.Rows[conIndex]["connection_string"].ToString();
ta = new Sta(connectionString);
}
catch
{
LbMessage.Text = "ไม่สามารถเชื่อมต่อฐานข้อมูล [" + conIndex + "]";
return;
}
// ทำการค้นหา token แล้วนำไปใส่ใน WebStateFactory หากไม่สำเร็จให้แสดง error และหยุดการทำงาน
try
{
string sqlToken = "";
sqlToken=@"
select
s.token_id,
s.application,
app.description,
app.workdate,
app.closeday_status,
app.used_flag,
c.coop_name,
c.coop_control,
c.cooplogo_path,
s.coop_id,
s.client_ip,
s.con_index,
s.username,
s.password,
s.create_time,
s.last_try
from
ssotoken s,
amappstatus app,
cmcoopmaster c
where
s.coop_id = c.coop_id and
s.coop_id = app.coop_id (+) and
s.application = app.application (+) and
s.token_password={0} and
s.session_id={1} and
s.token_lock=1 ";
if (Sta.IS_MYSQL_MODE)
{
sqlToken = @"
select
s.token_id,
s.application,
app.description,
app.workdate,
app.closeday_status,
app.used_flag,
c.coop_name,
c.coop_control,
c.cooplogo_path,
s.coop_id,
s.client_ip,
s.con_index,
s.username,
s.password,
s.create_time,
s.last_try
from
ssotoken s
left join amappstatus app on (s.coop_id = app.coop_id and s.application = app.application ),
cmcoopmaster c
where
s.coop_id = c.coop_id and
s.token_password={0} and
s.session_id={1} and
s.token_lock=1 ";
}
sqlToken = WebUtil.SQLFormat(sqlToken, tokenPassword, sessionId);
Sdt dt = ta.Query(sqlToken);
if (dt.Next())
{
new WebStateFactory(dt, xml, conIndex, connectionString);
string sqlUpdate = @"
update ssotoken set
token_lock = 0,
last_try = sysdate
where token_id = '" + dt.GetString("token_id") + "'";
ta.Exe(sqlUpdate);
ta.Close();
if (string.IsNullOrEmpty(dt.GetString("application")))
{
Response.Redirect("ApplicationSelectionPage.aspx");
}
else
{
Response.Redirect(Session["SsRestoreUrl"].ToString());
}
}
else
{
throw new Exception("ไม่สามารถยืนยันตัวตนผู้ใช้งานได้");
}
}
catch (Exception ex)
{
try
{
ta.Close();
}
catch { }
LbMessage.Text = ex.Message;
return;
}
try
{
ta.Close();
}
catch { }
}
}
}