¶Ô±ÈÐÂÎļþ |
| | |
| | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.IdentityModel.Tokens.Jwt; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Security.Claims; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.Const; |
| | | |
| | | namespace WIDESEA_Core.Authorization |
| | | { |
| | | public static class AuthorizationResponse |
| | | { |
| | | public static AuthorizationFilterContext FilterResult( |
| | | this AuthorizationFilterContext context, |
| | | HttpStatusCode statusCode, |
| | | string message = null) |
| | | { |
| | | context.Result = new ContentResult() |
| | | { |
| | | Content = new { message, status = false, code = (int)statusCode }.Serialize(), |
| | | ContentType = "application/json", |
| | | StatusCode = (int)statusCode |
| | | }; |
| | | //Logger.Info(LoggerType.ApiAuthorize, message); |
| | | return context; |
| | | } |
| | | public static AuthorizationFilterContext Unauthorized(this AuthorizationFilterContext context, string message = null) |
| | | { |
| | | return context.FilterResult(HttpStatusCode.Unauthorized, message); |
| | | } |
| | | //ä¸éè¿JWTéªè¯çï¼ç´æ¥å°ç¨æ·ä¿¡æ¯ç¼åèµ·æ¥ |
| | | public static void AddIdentity(this AuthorizationFilterContext context, int? userId = null) |
| | | { |
| | | int _userId = userId ?? JwtHelper.GetUserId(context.HttpContext.Request.Headers[AppSecret.TokenHeaderName]); |
| | | if (_userId <= 0) return; |
| | | //å°ç¨æ·Idç¼åå°ä¸ä¸æ(æè
èªå®ä¸ä¸ªå¯¹è±¡ï¼éè¿DI以AddScopedæ¹å¼æ³¨å
¥ä¸ä¸ææ¥ç®¡çç¨æ·ä¿¡æ¯) |
| | | var claims = new Claim[] { new Claim(JwtRegisteredClaimNames.Jti, _userId.ToString()) }; |
| | | context.HttpContext.User.AddIdentity(new ClaimsIdentity(claims)); |
| | | } |
| | | } |
| | | } |