firts commit
This commit is contained in:
1002
KART_API/Utils/Common.cs
Normal file
1002
KART_API/Utils/Common.cs
Normal file
File diff suppressed because it is too large
Load Diff
616
KART_API/Utils/MSDAL.cs
Normal file
616
KART_API/Utils/MSDAL.cs
Normal file
@@ -0,0 +1,616 @@
|
||||
using System.Data.SqlClient;
|
||||
using System.Data;
|
||||
|
||||
namespace KART_API.Utils
|
||||
{
|
||||
public class MSDAL
|
||||
{
|
||||
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MSDAL));
|
||||
|
||||
/// <summary>
|
||||
/// Config 파일 로드
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IConfiguration GetConfiguration()
|
||||
{
|
||||
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Report DB 연결
|
||||
/// </summary>
|
||||
/// <returns>DB 연결 객체</returns>
|
||||
public SqlConnection ConnectReport()
|
||||
{
|
||||
SqlConnection connection = new SqlConnection();
|
||||
try
|
||||
{
|
||||
var configuration = GetConfiguration();
|
||||
connection.ConnectionString = configuration.GetSection("ConnectionStrings").GetSection("KartString").Value;
|
||||
connection.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in ConnectReport : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// KON / KIWI DB 연결
|
||||
/// </summary>
|
||||
/// <param name="isKiwi">0:kon 1:kiwi</param>
|
||||
/// <returns>DB 연결 객체</returns>
|
||||
public SqlConnection ConnectKonKiwi(int isKiwi)
|
||||
{
|
||||
SqlConnection connection = new SqlConnection();
|
||||
string connectionString = string.Empty;
|
||||
try
|
||||
{
|
||||
var configuration = GetConfiguration();
|
||||
|
||||
if (isKiwi == 0)
|
||||
{
|
||||
connectionString = configuration.GetSection("ConnectionStrings").GetSection("KonString").Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
connectionString = configuration.GetSection("ConnectionStrings").GetSection("KiwiString").Value;
|
||||
}
|
||||
|
||||
connection.ConnectionString = connectionString;
|
||||
connection.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in ConnectKonKiwi : " + connectionString + "///" + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 인트라넷 DB 연결
|
||||
/// </summary>
|
||||
/// <returns>DB 연결 객체</returns>
|
||||
public SqlConnection ConnectIntra()
|
||||
{
|
||||
SqlConnection connection = new SqlConnection();
|
||||
try
|
||||
{
|
||||
var configuration = GetConfiguration();
|
||||
connection.ConnectionString = configuration.GetSection("ConnectionStrings").GetSection("IntraString").Value;
|
||||
connection.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in ConnectIntra : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DB 연결
|
||||
/// </summary>
|
||||
/// <param name="connectionString">Connection String</param>
|
||||
/// <returns>DB 연결 객체</returns>
|
||||
public SqlConnection Connect(string connectionString)
|
||||
{
|
||||
SqlConnection connection = new SqlConnection();
|
||||
try
|
||||
{
|
||||
connection.ConnectionString = connectionString;
|
||||
connection.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in Connect : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DB 연결 해제
|
||||
/// </summary>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
public void Disconnect(SqlConnection connection)
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Close();
|
||||
connection.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in Disconnect : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DB접속객체에서 Procedure 실행하여 DataReader로 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>결과 행이 저장되어 있는 DataReader 객체</returns>
|
||||
public SqlDataReader GetDataReader(string procedureName, SqlConnection connection, List<SqlParameter> sqlParameter)
|
||||
{
|
||||
SqlDataReader reader = null;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Count; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataReader로 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>결과 행이 저장되어 있는 DataReader 객체</returns>
|
||||
public SqlDataReader GetDataReaderWithProcedure(string procedureName, SqlConnection connection)
|
||||
{
|
||||
SqlDataReader reader = null;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문을 수행하여 DataReader로 반환
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL문</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>결과 행이 저장되어 있는 DataReader 객체</returns>
|
||||
public SqlDataReader GetDataReader(string sql, SqlConnection connection)
|
||||
{
|
||||
SqlDataReader reader = null;
|
||||
/*
|
||||
string sessionID = hcAccessor.HttpContext.Session.Id;
|
||||
//killProcessQuery(sessionID, connection);
|
||||
sql = "--" + sessionID + "--" + Environment.NewLine + sql;
|
||||
*/
|
||||
SqlCommand cmd = new SqlCommand(sql, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.Text;
|
||||
reader = cmd.ExecuteReader();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>프로시저 수행 결과 DataTable</returns>
|
||||
public DataTable GetDataTable(string procedureName, SqlConnection connection, SqlParameter[] sqlParameter)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
SqlDataReader reader = null;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Length; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 파라미터 없이 프로시저를 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataTableNonePara(string procedureName, SqlConnection connection)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
SqlDataReader reader = null;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수 List</param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataTable(string procedureName, SqlConnection connection, List<SqlParameter> sqlParameter)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
SqlDataReader reader = null;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Count; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataTable(" + procedureName + ") : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문을 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL문</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>SQL문 수행 결과 DataTable</returns>
|
||||
public DataTable GetDataTable(string sql, SqlConnection connection)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
SqlDataReader reader = null;
|
||||
/*
|
||||
string sessionID = hcAccessor.HttpContext.Session.Id;
|
||||
//killProcessQuery(sessionID, connection);
|
||||
sql = "--" + sessionID + "--" + Environment.NewLine + sql;
|
||||
*/
|
||||
SqlCommand cmd = new SqlCommand(sql, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.Text;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataTable : " + Environment.NewLine + sql + Environment.NewLine + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataSet 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객제</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>프로시저를 수행결과 DataSet</returns>
|
||||
public DataSet GetDataSet(string procedureName, SqlConnection connection, SqlParameter[] sqlParameter)
|
||||
{
|
||||
SqlDataAdapter adapter = null;
|
||||
DataSet ds = new DataSet();
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Length; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
adapter = new SqlDataAdapter(cmd);
|
||||
adapter.Fill(ds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
adapter.Dispose();
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문을 수행하여 DataSet 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객제</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>프로시저를 수행결과 DataSet</returns>
|
||||
public DataSet GetDataSet(string sql, SqlConnection connection)
|
||||
{
|
||||
SqlDataAdapter adapter = null;
|
||||
DataSet ds = new DataSet();
|
||||
SqlCommand cmd = new SqlCommand(sql, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.Text;
|
||||
adapter = new SqlDataAdapter(cmd);
|
||||
adapter.Fill(ds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
adapter.Dispose();
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문 실행
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL문</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteSQL(string sql, SqlConnection connection)
|
||||
{
|
||||
int affectedRow = 0;
|
||||
|
||||
/*
|
||||
string sessionID = hcAccessor.HttpContext.Session.Id;
|
||||
//killProcessQuery(sessionID, connection);
|
||||
sql = "--" + sessionID + "--" + Environment.NewLine + sql;
|
||||
*/
|
||||
|
||||
try
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand(sql, connection);
|
||||
cmd.CommandType = System.Data.CommandType.Text;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
cmd.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
affectedRow = -1;
|
||||
logger.Info("MSDAL class exception in ExecuteSQL : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저 수행
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수 배열</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteStoredProcedure(string procedureName, SqlConnection connection, SqlParameter[] sqlParameter)
|
||||
{
|
||||
int affectedRow = -1;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
for (int i = 0; i < sqlParameter.Length; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
|
||||
affectedRow = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저 수행
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수 List</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteStoredProcedure(string procedureName, SqlConnection connection, List<SqlParameter> sqlParameter)
|
||||
{
|
||||
int affectedRow = -1;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
for (int i = 0; i < sqlParameter.Count; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
|
||||
affectedRow = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저 수행
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteStoredProcedure(string procedureName, SqlConnection connection)
|
||||
{
|
||||
int affectedRow = -1;
|
||||
|
||||
SqlCommand cmd = new SqlCommand(procedureName, connection);
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
affectedRow = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 쿼리 강제로 KILL
|
||||
/// </summary>
|
||||
/// <param name="sessionID">Session ID</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
public void killProcessQuery(string sessionID, SqlConnection connection)
|
||||
{
|
||||
string killQuery = "declare @spid int" + Environment.NewLine;
|
||||
killQuery += " select @spid = session_id" + Environment.NewLine;
|
||||
killQuery += " from sys.dm_exec_requests handle" + Environment.NewLine;
|
||||
killQuery += " outer apply sys.fn_get_sql(handle.sql_handle) spname" + Environment.NewLine;
|
||||
killQuery += " where spname.text is not null" + Environment.NewLine;
|
||||
killQuery += " and spname.text like '%" + sessionID + "%'" + Environment.NewLine;
|
||||
killQuery += " and spname.text not like '%from sys.dm_exec_requests handle%'" + Environment.NewLine;
|
||||
killQuery += " if @spid <> ''" + Environment.NewLine;
|
||||
killQuery += " begin" + Environment.NewLine;
|
||||
killQuery += " exec('kill '+@spid)" + Environment.NewLine;
|
||||
killQuery += " end" + Environment.NewLine;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
SqlCommand cmd = new SqlCommand(killQuery, connection);
|
||||
cmd.CommandType = System.Data.CommandType.Text;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
cmd.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MSDAL class exception in killProcessQuery : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
513
KART_API/Utils/MYDAL.cs
Normal file
513
KART_API/Utils/MYDAL.cs
Normal file
@@ -0,0 +1,513 @@
|
||||
using System.Data;
|
||||
using MySqlConnector;
|
||||
|
||||
namespace KART_API.Utils
|
||||
{
|
||||
public class MYDAL
|
||||
{
|
||||
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MSDAL));
|
||||
|
||||
/// <summary>
|
||||
/// Config 파일 로드
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IConfiguration GetConfiguration()
|
||||
{
|
||||
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 새싹 DB 연결
|
||||
/// </summary>
|
||||
/// <returns>DB 연결 객체</returns>
|
||||
public MySqlConnection ConnectNewssak()
|
||||
{
|
||||
MySqlConnection connection = new MySqlConnection();
|
||||
string connectionString = string.Empty;
|
||||
try
|
||||
{
|
||||
var configuration = GetConfiguration();
|
||||
connection.ConnectionString = configuration.GetSection("ConnectionStrings").GetSection("NewssakString").Value;
|
||||
connection.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in ConnectKonKiwi : " + connectionString + "///" + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DB 연결
|
||||
/// </summary>
|
||||
/// <param name="connectionString">Connection String</param>
|
||||
/// <returns>DB 연결 객체</returns>
|
||||
public MySqlConnection Connect(string connectionString)
|
||||
{
|
||||
MySqlConnection connection = new MySqlConnection();
|
||||
try
|
||||
{
|
||||
connection.ConnectionString = connectionString;
|
||||
connection.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in Connect : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DB 연결 해제
|
||||
/// </summary>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
public void Disconnect(MySqlConnection connection)
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.Close();
|
||||
connection.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in Disconnect : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DB접속객체에서 Procedure 실행하여 DataReader로 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>결과 행이 저장되어 있는 DataReader 객체</returns>
|
||||
public MySqlDataReader GetDataReader(string procedureName, MySqlConnection connection, List<MySqlParameter> sqlParameter)
|
||||
{
|
||||
MySqlDataReader reader = null;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Count; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataReader로 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>결과 행이 저장되어 있는 DataReader 객체</returns>
|
||||
public MySqlDataReader GetDataReaderWithProcedure(string procedureName, MySqlConnection connection)
|
||||
{
|
||||
MySqlDataReader reader = null;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문을 수행하여 DataReader로 반환
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL문</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>결과 행이 저장되어 있는 DataReader 객체</returns>
|
||||
public MySqlDataReader GetDataReader(string sql, MySqlConnection connection)
|
||||
{
|
||||
MySqlDataReader reader = null;
|
||||
MySqlCommand cmd = new MySqlCommand(sql, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.Text;
|
||||
reader = cmd.ExecuteReader();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>프로시저 수행 결과 DataTable</returns>
|
||||
public DataTable GetDataTable(string procedureName, MySqlConnection connection, MySqlParameter[] sqlParameter)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
MySqlDataReader reader = null;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Length; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 파라미터 없이 프로시저를 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataTableNonePara(string procedureName, MySqlConnection connection)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
MySqlDataReader reader = null;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수 List</param>
|
||||
/// <returns></returns>
|
||||
public DataTable GetDataTable(string procedureName, MySqlConnection connection, List<MySqlParameter> sqlParameter)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
MySqlDataReader reader = null;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Count; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataTable(" + procedureName + ") : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문을 수행하여 DataTable 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL문</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>SQL문 수행 결과 DataTable</returns>
|
||||
public DataTable GetDataTable(string sql, MySqlConnection connection)
|
||||
{
|
||||
DataTable dataTable = new DataTable();
|
||||
MySqlDataReader reader = null;
|
||||
MySqlCommand cmd = new MySqlCommand(sql, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.Text;
|
||||
reader = cmd.ExecuteReader();
|
||||
dataTable.Load(reader);
|
||||
reader.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataTable : " + Environment.NewLine + sql + Environment.NewLine + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저를 수행하여 DataSet 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객제</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>프로시저를 수행결과 DataSet</returns>
|
||||
public DataSet GetDataSet(string procedureName, MySqlConnection connection, MySqlParameter[] sqlParameter)
|
||||
{
|
||||
MySqlDataAdapter adapter = null;
|
||||
DataSet ds = new DataSet();
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < sqlParameter.Length; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
adapter = new MySqlDataAdapter(cmd);
|
||||
adapter.Fill(ds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
adapter.Dispose();
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문을 수행하여 DataSet 객체 반환
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객제</param>
|
||||
/// <param name="sqlParameter">인수배열</param>
|
||||
/// <returns>프로시저를 수행결과 DataSet</returns>
|
||||
public DataSet GetDataSet(string sql, MySqlConnection connection)
|
||||
{
|
||||
MySqlDataAdapter adapter = null;
|
||||
DataSet ds = new DataSet();
|
||||
MySqlCommand cmd = new MySqlCommand(sql, connection);
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.Text;
|
||||
adapter = new MySqlDataAdapter(cmd);
|
||||
adapter.Fill(ds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
adapter.Dispose();
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SQL문 실행
|
||||
/// </summary>
|
||||
/// <param name="sql">SQL문</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteSQL(string sql, MySqlConnection connection)
|
||||
{
|
||||
int affectedRow = 0;
|
||||
try
|
||||
{
|
||||
MySqlCommand cmd = new MySqlCommand(sql, connection);
|
||||
cmd.CommandType = System.Data.CommandType.Text;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
cmd.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
affectedRow = -1;
|
||||
logger.Info("MYDAL class exception in ExecuteSQL : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저 수행
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수 배열</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteStoredProcedure(string procedureName, MySqlConnection connection, MySqlParameter[] sqlParameter)
|
||||
{
|
||||
int affectedRow = -1;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
for (int i = 0; i < sqlParameter.Length; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
|
||||
affectedRow = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저 수행
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <param name="sqlParameter">인수 List</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteStoredProcedure(string procedureName, MySqlConnection connection, List<MySqlParameter> sqlParameter)
|
||||
{
|
||||
int affectedRow = -1;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
for (int i = 0; i < sqlParameter.Count; i++)
|
||||
{
|
||||
cmd.Parameters.Add(sqlParameter[i]);
|
||||
}
|
||||
|
||||
affectedRow = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 프로시저 수행
|
||||
/// </summary>
|
||||
/// <param name="procedureName">프로시저명</param>
|
||||
/// <param name="connection">DB 연결 객체</param>
|
||||
/// <returns>실패 : -1</returns>
|
||||
public int ExecuteStoredProcedure(string procedureName, MySqlConnection connection)
|
||||
{
|
||||
int affectedRow = -1;
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
|
||||
|
||||
try
|
||||
{
|
||||
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||||
cmd.CommandTimeout = 300;
|
||||
|
||||
affectedRow = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Info("MYDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Dispose();
|
||||
}
|
||||
|
||||
return affectedRow;
|
||||
}
|
||||
}
|
||||
}
|
||||
216
KART_API/Utils/Security.cs
Normal file
216
KART_API/Utils/Security.cs
Normal file
@@ -0,0 +1,216 @@
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using KART_API.Dto;
|
||||
|
||||
namespace KART_API.Utils
|
||||
{
|
||||
public class Security
|
||||
{
|
||||
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Security));
|
||||
private static readonly string keyString = "kriT#3415%130$^$tuDi0_Ds34155100";//32글자
|
||||
private static readonly string keyString_iv = keyString.Substring(0, 16);//16byte "asxnWolsZy98kCfWuyrnqg==";
|
||||
|
||||
/// <summary>
|
||||
/// 문자열 암호화(AES256)
|
||||
/// </summary>
|
||||
/// <param name="InputText">암호화 할 String</param>
|
||||
/// <returns></returns>
|
||||
public string encryptString(string InputText)
|
||||
{
|
||||
byte[] encrypted;
|
||||
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
|
||||
{
|
||||
aes.KeySize = 256;
|
||||
aes.BlockSize = 128;
|
||||
aes.Key = Encoding.UTF8.GetBytes(keyString);;
|
||||
aes.IV = Encoding.UTF8.GetBytes(keyString_iv);
|
||||
aes.Mode = CipherMode.CBC;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
|
||||
ICryptoTransform enc = aes.CreateEncryptor(aes.Key, aes.IV);
|
||||
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write))
|
||||
{
|
||||
using (StreamWriter sw = new StreamWriter(cs))
|
||||
{
|
||||
sw.Write(InputText);
|
||||
}
|
||||
|
||||
encrypted = ms.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Convert.ToBase64String(encrypted);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 문자열 복호화(AES256)
|
||||
/// </summary>
|
||||
/// <param name="InputText">복호화 할 String</param>
|
||||
/// <returns></returns>
|
||||
public string decryptString(string InputText)
|
||||
{
|
||||
string decrypted = String.Empty;
|
||||
byte[] cipher = Convert.FromBase64String(InputText);
|
||||
|
||||
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
|
||||
{
|
||||
aes.KeySize = 256; //AES256
|
||||
aes.BlockSize = 128;
|
||||
aes.Key = Encoding.UTF8.GetBytes(keyString);
|
||||
aes.IV = Encoding.UTF8.GetBytes(keyString_iv);
|
||||
aes.Mode = CipherMode.CBC;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
|
||||
ICryptoTransform dec = aes.CreateDecryptor(aes.Key, aes.IV);
|
||||
|
||||
using (MemoryStream ms = new MemoryStream(cipher))
|
||||
{
|
||||
using (CryptoStream cs = new CryptoStream(ms, dec, CryptoStreamMode.Read))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(cs))
|
||||
{
|
||||
decrypted = sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
public string GenerateToken(string userID, string userName, string userTeam, string isAdmin)
|
||||
{
|
||||
// 암호문을 바이트 배열로 변환
|
||||
byte[] secretArray = Encoding.UTF8.GetBytes(keyString);
|
||||
|
||||
// 암호를 바탕으로 대칭키 알고리즘에 사용될 보안키 객체 생성.
|
||||
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(secretArray);
|
||||
|
||||
// 토큰 생성에 필요한 클레임과 서명 방식을 기술하는 객체
|
||||
SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
// ClaimsIdentity(Claim[] claims) 생성자 호출
|
||||
Subject = new ClaimsIdentity(
|
||||
// 클레임 배열
|
||||
new[] {
|
||||
// Claim(string type, string value) 클레임의 유형과 값을 이용하여 클레임 객채를 생성한다.
|
||||
// ClaimTypes는 클레임의 이름이 의미하는 바를 통일하도록 미리 정한 것이다.
|
||||
// ClaimTypes.Name은 수혜자의 이름을 나타내기 위한 클레임 유형이다.
|
||||
new Claim(ClaimTypes.Sid, userID),
|
||||
new Claim(ClaimTypes.Name, userName),
|
||||
new Claim(ClaimTypes.GroupSid, userTeam),
|
||||
new Claim(ClaimTypes.Role, isAdmin)
|
||||
|
||||
}),
|
||||
Expires = DateTime.UtcNow.AddHours(4),
|
||||
//Expires = DateTime.UtcNow.AddSeconds(10),
|
||||
|
||||
// JWT의 검증서명을 만드는데 필요한 객체를 지정한다. 보안키, 알고리즘이 지정되었다
|
||||
SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
|
||||
// JWT 처리자 객체로 토큰 객체를 생성하고 검증하는 역할.
|
||||
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
|
||||
|
||||
// 토큰 객체 생성
|
||||
JwtSecurityToken token = handler.CreateJwtSecurityToken(descriptor);
|
||||
|
||||
// 토큰 객체를 직렬화하여 반환.
|
||||
return handler.WriteToken(token);
|
||||
}
|
||||
|
||||
private static ClaimsPrincipal GetPrincipal(string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
|
||||
|
||||
JwtSecurityToken jwtToken = tokenHandler.ReadJwtToken(token);
|
||||
|
||||
if (jwtToken == null)
|
||||
return null;
|
||||
|
||||
byte[] secretArray = Encoding.UTF8.GetBytes(keyString);
|
||||
|
||||
TokenValidationParameters parameters = new TokenValidationParameters()
|
||||
{
|
||||
IssuerSigningKey = new SymmetricSecurityKey(secretArray),
|
||||
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
// set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
|
||||
SecurityToken securityToken;
|
||||
|
||||
ClaimsPrincipal principal = tokenHandler.ValidateToken(token, parameters, out securityToken);
|
||||
|
||||
return principal;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static UserInfo ValidateToken(string token)
|
||||
{
|
||||
ClaimsPrincipal entity = GetPrincipal(token);
|
||||
UserInfo userInfo;
|
||||
|
||||
if (entity == null)//KART 토큰을 해석하지 못하면 새싹 토큰으로 취급
|
||||
{
|
||||
JObject requestHeader = new JObject();
|
||||
requestHeader.Add("Authorization", "Bearer "+token);
|
||||
|
||||
string rquestValue = Common.callWebRequest("GET", "https://seed.kric.com:7443/api/v1/member/getInfo", requestHeader, null);
|
||||
if (Common.IsJson(rquestValue))
|
||||
{
|
||||
JObject requestJson = JObject.Parse(rquestValue);
|
||||
userInfo = new UserInfo(
|
||||
requestJson["member_id"].ToString(),
|
||||
requestJson["member_name"].ToString(),
|
||||
requestJson["teamInfo"]["groupCode"].ToString() + requestJson["teamInfo"]["code"].ToString(),
|
||||
requestJson["is_admin"].ToString()
|
||||
);/////ADMIN 등급 수정필요
|
||||
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
ClaimsIdentity identity;
|
||||
try
|
||||
{
|
||||
identity = (ClaimsIdentity)entity.Identity;
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Claim nameClaim = entity.FindFirst(ClaimTypes.Name); 도 동일한 효과
|
||||
Claim idClaim = identity.FindFirst(ClaimTypes.Sid);
|
||||
Claim nameClaim = identity.FindFirst(ClaimTypes.Name);
|
||||
Claim teamClaim = identity.FindFirst(ClaimTypes.GroupSid);
|
||||
Claim roleClaim = identity.FindFirst(ClaimTypes.Role);
|
||||
|
||||
userInfo = new UserInfo(idClaim.Value, nameClaim.Value, teamClaim.Value, roleClaim.Value);
|
||||
|
||||
return userInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user