123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using Microsoft.AspNetCore.Mvc;
- namespace IES.ExamServer.Controllers
- {
- public class BaseController : ControllerBase
- {
- public BaseController()
- {
- }
- public string GetIP()
- {
- var IpPort = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
- if (string.IsNullOrEmpty(IpPort))
- {
- IpPort = $"{HttpContext.Connection.RemoteIpAddress}";
- }
- if (IpPort.Contains("::"))
- {
- IpPort = "127.0.0.1";
- }
- return IpPort;
- }
- public string GetCookie(string key)
- {
- IRequestCookieCollection cookies = HttpContext.Request.Cookies;
- string value = "";
- if (cookies != null)
- {
- foreach (var ck in cookies)
- {
- if (ck.Key.Equals(key))
- {
- value = ck.Value;
- break;
- }
- }
- }
- return value;
- }
- /// <summary>
- /// 取得AuthToken權杖資訊
- /// </summary>
- /// <param name="key">Key Name</param>
- /// <returns></returns>
- public (string id, string? name, string picture, string school,string scope ,string timeZone,List<string> rolse, string keyData) GetAuthTokenInfo(string? key = null)
- {
- object? keyData = null;
- HttpContext.Items.TryGetValue("ID", out object? id);
- HttpContext.Items.TryGetValue("Name", out object? name);
- HttpContext.Items.TryGetValue("Picture", out object? picture);
- HttpContext.Items.TryGetValue("School", out object? school);
- HttpContext.Items.TryGetValue("Scope", out object? scope);
- HttpContext.Items.TryGetValue("TimeZone", out object? timeZone);
- List<string> rolse= new List<string>();
- if (HttpContext.Items.TryGetValue("Roles", out object? _roles))
- {
- if (_roles is List<string> s)
- {
- rolse=s;
- }
- }
- if (!string.IsNullOrWhiteSpace(key))
- {
- HttpContext.Items.TryGetValue(key, out keyData);
- }
- return ($"{id}", $"{name}", $"{picture}", $"{school}",$"{scope}",$"{timeZone}", rolse, $"{keyData}");
- }
-
- /// <summary>
- /// 取得驗證金鑰,Authorization
- /// </summary>
- public string GetToken()
- {
- return HttpContext.Request.Headers["Authorization"].ToString();
- }
- /// <summary>
- /// 取得JWT驗證金鑰,Authorization Bearer
- /// </summary>
- /// <param name="httpContext"></param>
- /// <returns></returns>
- public string GetJwtToken()
- {
- var token = string.Empty;
- string authorization = HttpContext.Request.Headers["Authorization"].ToString();
- if (!string.IsNullOrWhiteSpace(authorization) && authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
- {
- token = authorization.Substring("Bearer ".Length).Trim();
- }
- return token;
- }
- public int code = 0;
- public string msg = "OK";
- }
- }
|