public class AuthController : ApiController { [HttpGet] public HttpResponseMessage Token(string appid = "", string appsecret = "") { ApiResponseEntity rep; var isv = AppManage.Instance.GetAppISV(appid, appsecret); if (isv != null) { string token = TokenManage.Instance.CreateToken(appid); rep = new ApiResponseEntity { Status = InterfaceStatus.Success, BizData = new { AccessToken = token } }; } else { rep = new ApiResponseEntity() { Status = InterfaceStatus.Parm_Missing, Message = "param error" }; } return rep.ToHttpResponseMessage(); }}
public string CreateToken(string appid) { string token = Guid.NewGuid().ToString().ToMd5(); Set(token, appid); return token; }
public void Set(string token, string appid) { var config = ServerConfigManage.Instance.GetServerConfig(); string key = string.Format(RedisCacheKey.App_Token, token); RedisNetHelper.Set<string>(key, appid, DateTime.Now.AddSeconds(config.TokenSurvivalTime)); }
public class OAuthHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { ApiResponseEntity repEntity = null; string appid = ""; string ip = RequestHelper.GetWebClientIp(); if (!OAuthValidate.IpValidate(ip)) { repEntity = new ApiResponseEntity { Status = InterfaceStatus.IllegalIp, Message = "ip access limit" }; } else { string token = ""; string url = request.RequestUri.AbsoluteUri; var routeData = request.GetRouteData(); string controller = routeData.Values["controller"].ToString().ToLower(); string action = routeData.Values["action"].ToString().ToLower(); if (controller.Equals("auth") && action.Equals("token")) { return base.SendAsync(request, cancellationToken); } if (request.Method == HttpMethod.Get) { var query = request.RequestUri.ParseQueryString(); token = query["token"]; } if (token == null || token.Length == 0) { repEntity = new ApiResponseEntity { Status = InterfaceStatus.Token_Faild, Message = "token invalid" }; } else { appid = TokenManage.Instance.Get(token); if (appid == null || appid.Length == 0) { repEntity = new ApiResponseEntity { Status = InterfaceStatus.Token_Faild, Message = "token invalid" }; } else { if (!OAuthValidate.ApiValidate ( string.Format("{0}/{1}", controller, action), appid )) { repEntity = new ApiResponseEntity { Status = InterfaceStatus.No_Access, Message = "api access limit" }; } } } } if (repEntity != null) { var tsc = new TaskCompletionSource<HttpResponseMessage>(); tsc.SetResult(repEntity.ToHttpResponseMessage()); return tsc.Task; } else { return base.SendAsync(request, cancellationToken); } } }
public static bool IpValidate(string ip) { var config = ServerConfigManage.Instance.GetServerConfig(); bool isPass = true; if (isPass && config.IsStartIpWhiteList) { isPass = config.IpWhiteList.Contains(ip); } if (isPass && config.IsStartIpBlackList) { isPass = !config.IpBlackList.Contains(ip); } return isPass; } public static bool ApiValidate(string api, string appid) { var config = ServerConfigManage.Instance.GetServerConfig(); if (config.IsStartApiControl) { var apis = AppManage.Instance.GetAppApiResource(appid); return apis != null && apis.Contains(api); } return true; }
public static HttpResponseMessage ToHttpResponseMessage(this ResponseEntity rep, bool isEncrypt = false) { return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent ( isEncrypt ? EncryptHelper.Base64Replace(EncryptHelper.AESEncryptBase64(JsonHelper.ToJson(rep), Config.ApiEncryptKey)) : JsonHelper.ToJson(rep), System.Text.Encoding.UTF8, "application/json" ) }; }
联系客服