Skip to content

Commit ac31a33

Browse files
committed
study httpcontext -> get uid from it.
1 parent c4c0002 commit ac31a33

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

Blog.Core/Blog.Core.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Blog.Core/Controllers/PermissionController.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Blog.Core.AuthHelper.OverWrite;
56
using Blog.Core.Common.Helper;
67
using Blog.Core.IServices;
78
using Blog.Core.Model;
89
using Blog.Core.Model.Models;
910
using Microsoft.AspNetCore.Authorization;
11+
using Microsoft.AspNetCore.Http;
1012
using Microsoft.AspNetCore.Mvc;
1113

1214
namespace Blog.Core.Controllers
@@ -23,6 +25,7 @@ public class PermissionController : ControllerBase
2325
readonly IModuleServices _moduleServices;
2426
readonly IRoleModulePermissionServices _roleModulePermissionServices;
2527
readonly IUserRoleServices _userRoleServices;
28+
readonly IHttpContextAccessor _httpContext;
2629

2730
/// <summary>
2831
/// 构造函数
@@ -31,12 +34,14 @@ public class PermissionController : ControllerBase
3134
/// <param name="moduleServices"></param>
3235
/// <param name="roleModulePermissionServices"></param>
3336
/// <param name="userRoleServices"></param>
34-
public PermissionController(IPermissionServices permissionServices, IModuleServices moduleServices, IRoleModulePermissionServices roleModulePermissionServices, IUserRoleServices userRoleServices)
37+
/// <param name="httpContext"></param>
38+
public PermissionController(IPermissionServices permissionServices, IModuleServices moduleServices, IRoleModulePermissionServices roleModulePermissionServices, IUserRoleServices userRoleServices, IHttpContextAccessor httpContext)
3539
{
3640
_permissionServices = permissionServices;
3741
_moduleServices = moduleServices;
3842
_roleModulePermissionServices = roleModulePermissionServices;
3943
_userRoleServices = userRoleServices;
44+
_httpContext = httpContext;
4045

4146
}
4247

@@ -114,7 +119,7 @@ public async Task<MessageModel<PageModel<Permission>>> Get(int page = 1, string
114119
}
115120

116121
permissions.data = permissionsView;
117-
122+
118123
#endregion
119124

120125

@@ -263,12 +268,19 @@ orderby child.Id
263268
/// <param name="uid"></param>
264269
/// <returns></returns>
265270
[HttpGet]
266-
[AllowAnonymous]
267271
public async Task<MessageModel<NavigationBar>> GetNavigationBar(int uid)
268272
{
273+
269274
var data = new MessageModel<NavigationBar>();
270275

271-
if (uid > 0)
276+
// 两种方式获取 uid
277+
var uidInHttpcontext1 = (from item in _httpContext.HttpContext.User.Claims
278+
where item.Type == "jti"
279+
select item.Value).FirstOrDefault().ObjToInt();
280+
281+
var uidInHttpcontext = (JwtHelper.SerializeJwt(_httpContext.HttpContext.Request.Headers["Authorization"].ObjToString().Replace("Bearer ", "")))?.Uid;
282+
283+
if (uid > 0 && uid == uidInHttpcontext)
272284
{
273285
var roleId = ((await _userRoleServices.Query(d => d.IsDeleted == false && d.UserId == uid)).FirstOrDefault()?.RoleId).ObjToInt();
274286
if (roleId > 0)

Blog.Core/Startup.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using Microsoft.AspNetCore.Authorization;
2626
using Microsoft.AspNetCore.Builder;
2727
using Microsoft.AspNetCore.Hosting;
28+
using Microsoft.AspNetCore.Http;
2829
using Microsoft.AspNetCore.Mvc;
2930
using Microsoft.Extensions.Caching.Memory;
3031
using Microsoft.Extensions.Configuration;
@@ -62,16 +63,16 @@ public Startup(IConfiguration configuration)
6263
public IServiceProvider ConfigureServices(IServiceCollection services)
6364
{
6465
#region 部分服务注入-netcore自带方法
65-
//缓存注入
66+
// 缓存注入
6667
services.AddScoped<ICaching, MemoryCaching>();
6768
services.AddSingleton<IMemoryCache>(factory =>
6869
{
6970
var cache = new MemoryCache(new MemoryCacheOptions());
7071
return cache;
7172
});
72-
//Redis注入
73+
// Redis注入
7374
services.AddSingleton<IRedisCacheManager, RedisCacheManager>();
74-
//log日志注入
75+
// log日志注入
7576
services.AddSingleton<ILoggerHelper, LogHelper>();
7677
#endregion
7778

@@ -192,6 +193,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
192193
// 取消默认驼峰
193194
.AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });
194195

196+
197+
// Httpcontext 注入
198+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
195199
#endregion
196200

197201
services.AddSignalR();

0 commit comments

Comments
 (0)