using Infrastructure.Cache;
using Microsoft.AspNetCore.Http;
using OpenAuth.App.Interface;
using System;
using Infrastructure;
using Microsoft.Extensions.Options;
using OpenAuth.Repository.Domain;
namespace OpenAuth.App.SSO
{
///
/// 使用本地登录。这个注入IAuth时,只需要OpenAuth.Mvc一个项目即可,无需webapi的支持
///
public class LocalAuth : IAuth
{
private IHttpContextAccessor _httpContextAccessor;
private IOptions _appConfiguration;
private SysLogApp _logApp;
private AuthContextFactory _app;
private LoginParse _loginParse;
private ICacheContext _cacheContext;
public LocalAuth(IHttpContextAccessor httpContextAccessor
, AuthContextFactory app
, LoginParse loginParse
, ICacheContext cacheContext, IOptions appConfiguration, SysLogApp logApp)
{
_httpContextAccessor = httpContextAccessor;
_app = app;
_loginParse = loginParse;
_cacheContext = cacheContext;
_appConfiguration = appConfiguration;
_logApp = logApp;
}
///
/// 如果是Identity,则返回信息为用户账号
///
///
private string GetToken()
{
if (_appConfiguration.Value.IsIdentityAuth)
{
return _httpContextAccessor.HttpContext.User.Identity.Name;
}
string token = _httpContextAccessor.HttpContext.Request.Query[Define.TOKEN_NAME];
if (!String.IsNullOrEmpty(token)) return token;
token = _httpContextAccessor.HttpContext.Request.Headers[Define.TOKEN_NAME];
if (!String.IsNullOrEmpty(token)) return token;
var cookie = _httpContextAccessor.HttpContext.Request.Cookies[Define.TOKEN_NAME];
return cookie ?? String.Empty;
}
public bool CheckLogin(string token = "", string otherInfo = "")
{
if (_appConfiguration.Value.IsIdentityAuth)
{
return (!string.IsNullOrEmpty(_httpContextAccessor.HttpContext.User.Identity.Name));
}
if (string.IsNullOrEmpty(token))
{
token = GetToken();
}
if (string.IsNullOrEmpty(token))
{
return false;
}
try
{
var result = _cacheContext.Get(token) != null;
return result;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取当前登录的用户信息
/// 通过URL中的Token参数或Cookie中的Token
///
/// The account.
/// LoginUserVM.
public AuthStrategyContext GetCurrentUser()
{
if (_appConfiguration.Value.IsIdentityAuth)
{
return _app.GetAuthStrategyContext(GetToken());
}
AuthStrategyContext context = null;
var user = _cacheContext.Get(GetToken());
if (user != null)
{
context = _app.GetAuthStrategyContext(user.Account);
}
return context;
}
///
/// 获取当前登录的用户名
/// 通过URL中的Token参数或Cookie中的Token
///
/// The account.
/// System.String.
public string GetUserName(string otherInfo = "")
{
if (_appConfiguration.Value.IsIdentityAuth)
{
return _httpContextAccessor.HttpContext.User.Identity.Name;
}
var user = _cacheContext.Get(GetToken());
if (user != null)
{
return user.Account;
}
return "";
}
///
/// 登录接口
///
/// 应用程序key.
/// 用户名
/// 密码
/// System.String.
public LoginResult Login(string appKey, string username, string pwd)
{
if (_appConfiguration.Value.IsIdentityAuth)
{
return new LoginResult
{
Code = 500,
Message = "接口启动了OAuth认证,暂时不能使用该方式登录"
};
}
var result = _loginParse.Do(new PassportLoginRequest
{
AppKey = appKey,
Account = username,
Password = pwd
});
var log = new SysLog
{
Content = $"用户登录,结果:{result.Message}",
Result = result.Code == 200 ? 0 : 1,
CreateId = username,
CreateName = username,
TypeName = "登录日志"
};
_logApp.Add(log);
return result;
}
///
/// 注销,如果是Identity登录,需要在controller处理注销逻辑
///
public bool Logout()
{
var token = GetToken();
if (String.IsNullOrEmpty(token)) return true;
try
{
_cacheContext.Remove(token);
return true;
}
catch
{
return false;
}
}
}
}