2016-01-05 17:14:10 +08:00
|
|
|
|
using System;
|
2016-07-19 15:18:45 +08:00
|
|
|
|
using System.Configuration;
|
2015-11-30 17:44:42 +08:00
|
|
|
|
using System.Web.Mvc;
|
2016-10-14 11:22:16 +08:00
|
|
|
|
using Infrastructure;
|
2016-07-08 18:51:48 +08:00
|
|
|
|
using OpenAuth.App.SSO;
|
2017-12-24 17:55:02 +08:00
|
|
|
|
using System.Web;
|
2015-11-30 17:44:42 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.Mvc.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class LoginController : Controller
|
|
|
|
|
{
|
2016-07-19 15:18:45 +08:00
|
|
|
|
private string _appKey = ConfigurationManager.AppSettings["SSOAppKey"];
|
2015-11-30 17:44:42 +08:00
|
|
|
|
|
|
|
|
|
// GET: Login
|
|
|
|
|
public ActionResult Index()
|
|
|
|
|
{
|
2016-07-19 15:18:45 +08:00
|
|
|
|
ViewBag.AppKey = _appKey;
|
2015-11-30 17:44:42 +08:00
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2016-10-28 19:02:02 +08:00
|
|
|
|
public string Index(string username, string password)
|
2015-11-30 17:44:42 +08:00
|
|
|
|
{
|
2017-11-28 23:54:49 +08:00
|
|
|
|
var resp = new LoginResult();
|
2015-11-30 17:44:42 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-07-19 15:18:45 +08:00
|
|
|
|
var result = AuthUtil.Login(_appKey, username, password);
|
2017-12-24 17:55:02 +08:00
|
|
|
|
if (result.Code == 200)
|
2016-10-28 19:02:02 +08:00
|
|
|
|
{
|
2017-12-24 17:55:02 +08:00
|
|
|
|
|
|
|
|
|
var cookie = new HttpCookie("Token", result.Token)
|
|
|
|
|
{
|
|
|
|
|
Expires = DateTime.Now.AddDays(10)
|
|
|
|
|
};
|
|
|
|
|
Response.Cookies.Add(cookie);
|
|
|
|
|
resp.Result = "/home/index";
|
2016-10-28 19:02:02 +08:00
|
|
|
|
}
|
2016-07-08 18:51:48 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-10-12 16:38:46 +08:00
|
|
|
|
resp.Message = "登录失败";
|
2016-07-08 18:51:48 +08:00
|
|
|
|
}
|
2015-11-30 17:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-11-28 23:54:49 +08:00
|
|
|
|
resp.Code = 500;
|
2016-10-28 19:02:02 +08:00
|
|
|
|
resp.Message = e.Message;
|
2015-11-30 17:44:42 +08:00
|
|
|
|
}
|
2016-10-28 19:02:02 +08:00
|
|
|
|
return JsonHelper.Instance.Serialize(resp);
|
2015-11-30 17:44:42 +08:00
|
|
|
|
}
|
2015-12-01 17:30:24 +08:00
|
|
|
|
|
2018-04-07 00:36:26 +08:00
|
|
|
|
public string Login(string username, string password)
|
2015-12-01 17:30:24 +08:00
|
|
|
|
{
|
2018-03-31 14:45:39 +08:00
|
|
|
|
var resp = new Response();
|
2015-12-01 17:30:24 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-04-07 00:36:26 +08:00
|
|
|
|
var result = AuthUtil.Login(_appKey, username, password);
|
2017-12-24 17:55:02 +08:00
|
|
|
|
if (result.Code == 200)
|
|
|
|
|
{
|
|
|
|
|
var cookie = new HttpCookie("Token", result.Token)
|
|
|
|
|
{
|
|
|
|
|
Expires = DateTime.Now.AddDays(10)
|
|
|
|
|
};
|
|
|
|
|
Response.Cookies.Add(cookie);
|
|
|
|
|
}
|
2016-07-08 18:51:48 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2018-03-31 14:45:39 +08:00
|
|
|
|
resp.Code = 500;
|
|
|
|
|
resp.Message = result.Message;
|
2016-07-08 18:51:48 +08:00
|
|
|
|
}
|
2015-12-01 17:30:24 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-03-31 14:45:39 +08:00
|
|
|
|
resp.Code = 500;
|
|
|
|
|
resp.Message = e.Message;
|
2015-12-01 17:30:24 +08:00
|
|
|
|
}
|
2018-03-31 14:45:39 +08:00
|
|
|
|
|
|
|
|
|
return JsonHelper.Instance.Serialize(resp);
|
2015-12-01 17:30:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Logout()
|
|
|
|
|
{
|
2016-04-21 10:54:05 +08:00
|
|
|
|
|
2016-07-08 18:51:48 +08:00
|
|
|
|
AuthUtil.Logout();
|
2015-12-01 17:30:24 +08:00
|
|
|
|
return RedirectToAction("Index", "Login");
|
|
|
|
|
}
|
2015-11-30 17:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|