OpenAuth.Net/OpenAuth.WebApi/Areas/SSO/Controllers/CheckController.cs

113 lines
3.3 KiB
C#
Raw Normal View History

// ***********************************************************************
// Assembly : OpenAuth.WebApi
// Author : yubaolee
// Created : 07-11-2016
//
// Last Modified By : yubaolee
// Last Modified On : 07-11-2016
// Contact :
// File: CheckController.cs
// ***********************************************************************
2016-12-27 11:25:51 +08:00
using System;
2017-04-16 20:49:21 +08:00
using System.Web.Http;
2016-07-08 18:51:48 +08:00
using Infrastructure;
2016-12-27 11:25:51 +08:00
using Infrastructure.Cache;
2016-07-08 18:51:48 +08:00
using OpenAuth.App;
using OpenAuth.App.SSO;
using System.Web.Mvc;
2017-11-30 17:47:41 +08:00
using OpenAuth.App.Response;
2016-07-08 18:51:48 +08:00
namespace OpenAuth.WebApi.Areas.SSO.Controllers
{
/// <summary>
/// sso验证
/// <para>其他站点通过后台Post来认证</para>
/// <para>或使用静态类OpenAuth.App.SSO.AuthUtil访问</para>
/// </summary>
2017-06-04 01:11:28 +08:00
public class CheckController : ApiController
2016-07-08 18:51:48 +08:00
{
2017-11-29 18:26:36 +08:00
public AuthorizeApp _app { get; set; }
2016-12-27 11:25:51 +08:00
private ObjCacheProvider<UserAuthSession> _objCacheProvider = new ObjCacheProvider<UserAuthSession>();
2016-07-08 18:51:48 +08:00
2017-06-04 01:25:16 +08:00
/// <summary>
/// 检验token是否有效
/// </summary>
/// <param name="token">The token.</param>
/// <param name="requestid">备用参数.</param>
2017-06-04 01:11:28 +08:00
[System.Web.Mvc.HttpGet]
2017-06-04 01:25:16 +08:00
public bool GetStatus(string token, string requestid = "")
2016-07-08 18:51:48 +08:00
{
2016-12-27 11:25:51 +08:00
if (_objCacheProvider.GetCache(token) != null)
2016-07-08 18:51:48 +08:00
{
return true;
}
return false;
2017-06-04 01:11:28 +08:00
}
2017-06-04 01:25:16 +08:00
/// <summary>
/// 根据token获取用户及用户可访问的所有资源
/// </summary>
/// <param name="token"></param>
/// <param name="requestid">备用参数.</param>
2017-06-04 01:11:28 +08:00
[System.Web.Mvc.HttpGet]
2017-06-04 01:25:16 +08:00
public UserWithAccessedCtrls GetUser(string token, string requestid = "")
{
string userName = GetUserName(token, requestid);
if (!string.IsNullOrEmpty(userName))
{
2017-06-04 01:11:28 +08:00
return _app.GetAccessedControls(userName);
}
2017-06-04 01:11:28 +08:00
return null;
}
2017-06-04 01:25:16 +08:00
/// <summary>
/// 根据token获取用户名称
/// </summary>
/// <param name="token"></param>
/// <param name="requestid">备用参数.</param>
2017-06-04 01:11:28 +08:00
[System.Web.Mvc.HttpGet]
public string GetUserName(string token, string requestid = "")
2016-07-08 18:51:48 +08:00
{
2016-12-27 11:25:51 +08:00
var user = _objCacheProvider.GetCache(token);
2016-07-08 18:51:48 +08:00
if (user != null)
{
return user.UserName;
2016-07-08 18:51:48 +08:00
}
return string.Empty;
}
2017-06-04 01:25:16 +08:00
/// <summary>
/// 登录接口
/// </summary>
/// <param name="request">登录参数</param>
/// <returns></returns>
2017-04-16 20:49:21 +08:00
[System.Web.Mvc.HttpPost]
2017-06-04 01:11:28 +08:00
public LoginResult Login(PassportLoginRequest request)
{
2017-06-04 01:11:28 +08:00
return SSOAuthUtil.Parse(request);
2017-06-04 01:25:16 +08:00
}
/// <summary>
/// 注销登录
/// </summary>
/// <param name="token"></param>
/// <param name="requestid">备用参数.</param>
2017-04-16 20:49:21 +08:00
[System.Web.Mvc.HttpPost]
2017-06-04 01:25:16 +08:00
public bool Logout(string token, string requestid="")
2016-12-27 11:25:51 +08:00
{
try
{
_objCacheProvider.Remove(token);
return true;
}
catch (Exception)
{
return false;
}
}
2016-07-08 18:51:48 +08:00
}
}