324 lines
12 KiB
C#
324 lines
12 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Claims;
|
||
using System.Threading.Tasks;
|
||
using Air.Model.AirAdminViewModel;
|
||
using Microsoft.AspNetCore.Authentication;
|
||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Http.Authentication;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Mini.Common;
|
||
using Mini.Services.Bas;
|
||
using Mini.Services.ww;
|
||
using Mini.Web.WebHelper;
|
||
using Mini.Web.WebHelper.Models;
|
||
|
||
namespace Mini.Web.Areas.Admin.Controllers
|
||
{
|
||
public class AccountController : Controller
|
||
{
|
||
private IBasInnerUserService _innerUser;
|
||
private IBasInnerUserRoleService _inneruserRole;
|
||
private IBasRoleRightResourceService _roleRightResource;
|
||
private IBasRoleService _role;
|
||
private IBasLoginLogService _log;
|
||
private IHttpContextAccessor _accessor;
|
||
private Iww_hhuser_Service _wwhhuserService;
|
||
|
||
public AccountController(IBasInnerUserRoleService inneruserRole, IBasRoleRightResourceService roleRightResource, IBasInnerUserService innerUser, IBasRoleService role, IBasLoginLogService log, IHttpContextAccessor accessor, Iww_hhuser_Service wwhhuserService)
|
||
{
|
||
this._inneruserRole = inneruserRole;
|
||
this._roleRightResource = roleRightResource;
|
||
this._innerUser = innerUser;
|
||
this._role = role;
|
||
this._log = log;
|
||
this._wwhhuserService = wwhhuserService;
|
||
_accessor = accessor;
|
||
}
|
||
|
||
[NonAction]
|
||
[Area("Admin")]
|
||
private ActionResult RedirectByAuthentication(string returnUrl)
|
||
{
|
||
if (User.Identity.IsAuthenticated)
|
||
{
|
||
// 如果已经登录
|
||
if (null != returnUrl)
|
||
{
|
||
// 跳转到前一个访问的页面
|
||
return this.Redirect(returnUrl);
|
||
}
|
||
else
|
||
{
|
||
// 跳转到首页
|
||
return this.RedirectToAction("Index", "Home");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Bas_InnerUserModel user = null;
|
||
return View(user);
|
||
}
|
||
}
|
||
[HttpGet]
|
||
[Area("Admin")]
|
||
public ActionResult OutLogon(string eid, string returnUrl)
|
||
{
|
||
var peid = 0;
|
||
if (string.IsNullOrWhiteSpace(eid) || !int.TryParse(eid, out peid))
|
||
{
|
||
Response.Redirect(Url.Action("ErrorView", "Error", new { message = "错误提示:参数错误!" }));
|
||
}
|
||
var model = _innerUser.GetBasInnerUserByEid(peid);
|
||
if (model == null)
|
||
{
|
||
Response.Redirect(Url.Action("ErrorView", "Error", new { message = "错误提示:找不到用户!" }));
|
||
}
|
||
//添加登陆日志
|
||
_log.Log(new Model.Entity.Bas_LoginLog()
|
||
{
|
||
eid = model.eid,
|
||
ip = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(),
|
||
logintime = DateTime.Now,
|
||
uid = model.uid
|
||
});
|
||
return RedirectToMain(model.uid, model.eid, returnUrl, "");
|
||
}
|
||
|
||
[HttpGet]
|
||
[Area("Admin")]
|
||
public ActionResult LogOn(string returnUrl)
|
||
{
|
||
this.ViewBag.ReturnUrl = returnUrl;
|
||
|
||
//int int_error = _cacheQ.GetValue_Parameter(WX.CRM.Model.Enum.Parameter.Sys_Environment_LogOn).GetInt(1);
|
||
//if (Session["LogOnErrorNum"] != null && (int)Session["LogOnErrorNum"] >= int_error)
|
||
//{
|
||
// ViewBag.ShowSnCode = true;
|
||
//}
|
||
ViewBag.ShowSnCode = false;
|
||
return this.RedirectByAuthentication(returnUrl);
|
||
}
|
||
[Area("Admin")]
|
||
private ActionResult RedirectToMain(int userId, int EID, string returnUrl, string password)
|
||
{
|
||
var userrights = new UserRights();
|
||
var login = new LoginHelper(_inneruserRole, _roleRightResource, _innerUser, _role, _wwhhuserService);
|
||
//decimal logPkId = login.CreateLoginLog(userId, EID, fjh);
|
||
string userinfo = login.setUserinfo(userId, EID, out userrights);
|
||
string token = Utility.EncryptMD5(userinfo);
|
||
|
||
//var claims = new List<Claim>()
|
||
// {
|
||
// new Claim(ClaimTypes.Name,EID.ToString()) ,
|
||
// new Claim(ClaimTypes.UserData,userinfo) ,
|
||
// //,new Claim(ClaimTypes.Email,"emailaccount@microsoft.com")
|
||
// };
|
||
string rolecodes = "";
|
||
if (userrights.rights != null && userrights.rights.Length > 0)
|
||
{
|
||
rolecodes = "[" + string.Join("][", userrights.rights) + "]";
|
||
}
|
||
|
||
var claims = new List<Claim>
|
||
{
|
||
new Claim(ClaimTypes.Name,userId.ToString()),
|
||
new Claim(ClaimTypes.UserData,userinfo),
|
||
new Claim(ClaimTypes.Role,rolecodes)
|
||
};
|
||
//var userPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "SuperSecureLogin"));
|
||
//var userPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, token));
|
||
var claimsIdentity = new ClaimsIdentity(claims, "MyCookies");
|
||
TimeSpan expire = TimeSpan.FromHours(5);
|
||
var authProperties = new Microsoft.AspNetCore.Authentication.AuthenticationProperties()
|
||
{
|
||
//ExpiresUtc = DateTime.Now.Add(expire),
|
||
IsPersistent = true,
|
||
AllowRefresh = true,
|
||
};
|
||
HttpContext.SignInAsync("MyCookies", new ClaimsPrincipal(claimsIdentity), authProperties);
|
||
|
||
//HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties
|
||
//{
|
||
// ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
|
||
// IsPersistent = false,
|
||
// AllowRefresh = false
|
||
//});
|
||
//this.Response.Cookies.Add(cookie);
|
||
|
||
//写入认证信息
|
||
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
|
||
{
|
||
return Redirect(returnUrl);
|
||
}
|
||
else
|
||
{
|
||
return this.RedirectToAction("Index", "Home");
|
||
}
|
||
//if (userrights != null && userrights.rights != null && userrights.rights.Count() > 0)
|
||
//{
|
||
|
||
// //存入缓存
|
||
// string userInfoCache = "Cache_UserInfo_" + userId.ToString();
|
||
// CacheHelper.Set<UserRights>(userInfoCache, userrights);
|
||
// int checkpwd = Utility.PasswordStrength(password);
|
||
// if (checkpwd > 0)
|
||
// {
|
||
// return Redirect("/Admin/Account/simplePwd?type=" + checkpwd);
|
||
// }
|
||
// else
|
||
// {
|
||
// if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
|
||
// && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
|
||
// {
|
||
// return Redirect(returnUrl);
|
||
// }
|
||
// else
|
||
// {
|
||
// return this.RedirectToAction("Index", "Home");
|
||
// }
|
||
// }
|
||
//}
|
||
//else
|
||
//{
|
||
// return this.RedirectToAction("NoRightView", "Error");
|
||
//}
|
||
}
|
||
[HttpPost]
|
||
[Area("Admin")]
|
||
public ActionResult LogOn(Bas_InnerUserModel user, string returnUrl)
|
||
{
|
||
this.ViewBag.ReturnUrl = returnUrl;
|
||
try
|
||
{
|
||
//if (ModelState.IsValid)
|
||
//{
|
||
//if (Session["LogOnErrorNum"] != null && (Session["ValidateCode"] == null || user.SnCode == null || user.SnCode.ToUpper() != Session["ValidateCode"].ToString()))
|
||
//{
|
||
// this.ViewBag.LogOnError = "验证码错误!";
|
||
//}
|
||
//else
|
||
//{
|
||
var error = string.Empty;
|
||
var userId = 0;
|
||
bool success = this._innerUser.ValidateUser(user.eid, user.passwd, out error, out userId);
|
||
|
||
if (success)
|
||
{
|
||
//Session["LogOnErrorNum"] = null;
|
||
//remenberMe(user.EID, user.PASSWORD, RememberMe);
|
||
|
||
//添加登陆日志
|
||
_log.Log(new Model.Entity.Bas_LoginLog()
|
||
{
|
||
eid = user.eid,
|
||
ip = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(),
|
||
logintime = DateTime.Now,
|
||
uid = userId
|
||
});
|
||
return RedirectToMain(userId, user.eid, returnUrl, user.passwd);
|
||
}
|
||
else
|
||
{
|
||
//if (Session["LogOnErrorNum"] == null)
|
||
// Session["LogOnErrorNum"] = 1;
|
||
//else
|
||
//{
|
||
// Session["LogOnErrorNum"] = (int)Session["LogOnErrorNum"] + 1;
|
||
//}
|
||
|
||
this.ViewBag.LogOnError = error;
|
||
}
|
||
//}
|
||
//int int_error = _cacheQ.GetValue_Parameter(WX.CRM.Model.Enum.Parameter.Sys_Environment_LogOn).GetInt(1);
|
||
//if (Session["LogOnErrorNum"] != null && (int)Session["LogOnErrorNum"] >= int_error)
|
||
//{
|
||
// ViewBag.ShowSnCode = true;
|
||
//}
|
||
//Session["ValidateCode"] = null;
|
||
//}
|
||
//else
|
||
//{
|
||
// this.ViewBag.LogOnError = "验证不通过";
|
||
//}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.ViewBag.LogOnError = ex.Message;
|
||
LogHelper.Error("AccountController:" + ex.ToString());
|
||
}
|
||
return View();
|
||
}
|
||
|
||
|
||
[HttpGet]
|
||
[Area("Admin")]
|
||
// [NonAction]
|
||
public ActionResult LogOff()
|
||
{
|
||
string cacheKey = "Cache_UserInfo_" + this.HttpContext.User.Identity.Name;
|
||
CacheHelper.Remove(cacheKey);
|
||
//Session["AuthorizeSession"] = null;
|
||
//FormsAuthentication.SignOut();
|
||
HttpContext.SignOutAsync("MyCookies");
|
||
|
||
return RedirectToAction("LogOn", "Account");
|
||
}
|
||
[Area("Admin")]
|
||
public ActionResult simplePwd(int type = 0)
|
||
{
|
||
string msg = "";
|
||
if (type == 1)
|
||
msg = "你的密码属于纯数字,过于简单,请修改密码";
|
||
if (type == 2)
|
||
msg = "你的密码属于纯字母,过于简单,请修改密码";
|
||
if (type == 3)
|
||
msg = "你的密码小于6位,过于简单,请修改密码";
|
||
ViewBag.MSG = msg;
|
||
return View();
|
||
}
|
||
[Area("Admin")]
|
||
public ActionResult UnAuthorized(string type)
|
||
{
|
||
if (string.IsNullOrEmpty(type))
|
||
{
|
||
return View();
|
||
}
|
||
else
|
||
{
|
||
ValidationErrors errors = new ValidationErrors();
|
||
errors.Add("你没有权限操作!");
|
||
return JsonHandler.ManageMessage(errors, false);
|
||
}
|
||
}
|
||
|
||
[Area("Admin")]
|
||
/// <summary>
|
||
/// 随机生成验证码
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public ActionResult GetValidateCode()
|
||
{
|
||
HttpContext.Session.Set("ValidateCode", null);// ("uid", "1234");
|
||
|
||
//HttpContext.Session.TryGetValue["ValidateCode"] = null;
|
||
SnCodeHelper code = new SnCodeHelper();
|
||
//生成随机数字
|
||
string codeStr = Utility.CreateRandomSatl(5);
|
||
//保存在session 用于验证正确用
|
||
HttpContext.Session.SetString("ValidateCode", codeStr.ToUpper());
|
||
//生成图片
|
||
byte[] bytes = code.CreateValidateImage(codeStr);
|
||
return File(bytes, @"image/gif");
|
||
}
|
||
[Area("Admin")]
|
||
public ActionResult AccessDenied()
|
||
{
|
||
return View();
|
||
}
|
||
|
||
}
|
||
} |