86 lines
2.8 KiB
C#
86 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization.Json;
|
|
using System.Web;
|
|
using System.Web.SessionState;
|
|
using Dapper;
|
|
using Dapper.Contrib.Extensions;
|
|
|
|
|
|
namespace abbott_2024_event.BackEnd.api
|
|
{
|
|
/// <summary>
|
|
/// signin 的摘要描述
|
|
/// </summary>
|
|
public class signin : IHttpHandler, IReadOnlySessionState
|
|
{
|
|
SqlConnection conn = new SqlConnection(globalClass.appsettings("DBConnectionString"));
|
|
|
|
public void ProcessRequest(HttpContext context)
|
|
{
|
|
result objRet = new result();
|
|
DataContractJsonSerializer json = new DataContractJsonSerializer(objRet.GetType());
|
|
context.Response.ContentType = "application/json;charset=utf-8";
|
|
|
|
string id = (context.Request["id"] == null) ? "" : context.Request["id"].ToString();
|
|
string pwd = (context.Request["pwd"] == null) ? "" : context.Request["pwd"].ToString();
|
|
|
|
login login = conn.QueryFirstOrDefault<login>("select * from login where login_id = @login_id and login_pwd = @login_pwd", new { login_id = id, login_pwd = pwd });
|
|
|
|
if (login == null)
|
|
{
|
|
objRet.ret = "no";
|
|
objRet.err_code = "0001";
|
|
objRet.message = "帳號或密碼錯誤";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
return;
|
|
}
|
|
|
|
string token_key = globalClass.CreateRandomCode(32);
|
|
string user_uid = id;
|
|
|
|
token newToken = new token();
|
|
newToken.user_uid = user_uid;
|
|
newToken.user_id = id;
|
|
newToken.token_key = token_key;
|
|
newToken.token_isremember = "N";
|
|
newToken.token_expireddate = DateTime.Now.AddMinutes(60);
|
|
newToken.token_createdate = DateTime.Now;
|
|
|
|
HttpCookie tokenCookie = new HttpCookie("token");
|
|
HttpCookie idCookie = new HttpCookie("id");
|
|
tokenCookie["token"] = token_key;
|
|
tokenCookie["uid"] = user_uid;
|
|
idCookie["id"] = id;
|
|
|
|
tokenCookie.Expires = DateTime.Now.AddMinutes(60);
|
|
idCookie.Expires = DateTime.Now.AddDays(31);
|
|
|
|
conn.Insert<token>(newToken);
|
|
|
|
context.Response.Cookies.Add(tokenCookie);
|
|
context.Response.Cookies.Add(idCookie);
|
|
|
|
objRet.ret = "yes";
|
|
json.WriteObject(context.Response.OutputStream, objRet);
|
|
}
|
|
|
|
public class result
|
|
{
|
|
public string ret = "no";
|
|
public string err_code = "0000";
|
|
public string message = "";
|
|
}
|
|
|
|
|
|
public bool IsReusable
|
|
{
|
|
get
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |