3 回答
TA贡献1909条经验 获得超7个赞
public class HomeController : Controller
{
private readonly DataContext _context;
private readonly IRepository _repo;
public HomeController(DataContext context, IRepository repo)
{
this._context = context;
this._repo = repo;
}
public int GetUserId()
{
return (int)HttpContext.Session.GetInt32("ID");
}
TA贡献1827条经验 获得超8个赞
在 ASP.NET Core 中管理会话(全局)的最佳方法是:
1)。添加静态类 SessionExtensions.cs
public static class SessionExtensions
{
public static void SetObject(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetObject<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
public static void SetBoolean(this ISession session, string key, bool value)
{
session.Set(key, BitConverter.GetBytes(value));
}
public static bool? GetBoolean(this ISession session, string key)
{
var data = session.Get(key);
if (data == null)
{
return null;
}
return BitConverter.ToBoolean(data, 0);
}
public static void SetDouble(this ISession session, string key, double value)
{
session.Set(key, BitConverter.GetBytes(value));
}
public static double? GetDouble(this ISession session, string key)
{
var data = session.Get(key);
if (data == null)
{
return null;
}
return BitConverter.ToDouble(data, 0);
}
}
2)。添加静态类 UserSession.cs
public static class UserSession
{
private static IHttpContextAccessor _accessor;
public static void Configure(IHttpContextAccessor httpContextAccessor)
{
_accessor = httpContextAccessor;
}
public static HttpContext HttpContext => _accessor.HttpContext;
public static Guid UserId
{
get
{
if (HttpContext.Session.GetString("UserId") == null || Guid.Parse(HttpContext.Session.GetString("UserId")) == Guid.Empty)
return Guid.Empty;
else
return Guid.Parse(Convert.ToString(HttpContext.Session.GetString("UserId")));
}
set
{
HttpContext.Session.SetString("UserId", Convert.ToString(value));
}
}
public static byte[] ProfileImage
{
get
{
if (HttpContext.Session.GetObject<byte[]>("ProfileImage") == null)
return null;
else
return HttpContext.Session.GetObject<byte[]>("ProfileImage");
}
set
{
HttpContext.Session.SetObject("ProfileImage", value);
}
}
public static string JwtToken
{
get
{
if (HttpContext.Session.GetString("JwtToken") == null)
return null;
else
return HttpContext.Session.GetString("JwtToken");
}
set
{
HttpContext.Session.SetString("JwtToken", value);
}
}
public static string FirstName
{
get
{
if (HttpContext.Session.GetString("FirstName") == null)
return null;
else
return HttpContext.Session.GetString("FirstName");
}
set
{
HttpContext.Session.SetString("FirstName", value);
}
}
public static string LastName
{
get
{
if (HttpContext.Session.GetString("LastName") == null)
return null;
else
return HttpContext.Session.GetString("LastName");
}
set
{
HttpContext.Session.SetString("LastName", value);
}
}
public static string FullName { get { return (!string.IsNullOrWhiteSpace(FirstName) ? FirstName : string.Empty) + " " + (!string.IsNullOrWhiteSpace(LastName) ? LastName : string.Empty); } }
public static string Email
{
get
{
if (HttpContext.Session.GetString("Email") == null)
return null;
else
return HttpContext.Session.GetString("Email");
}
set
{
HttpContext.Session.SetString("Email", value);
}
}
}
3).在startup.cs中添加这个
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(60);//You can set Time
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
UserSession.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());
}
4)。瞧。完毕
--------------------
How to use :
Set session (Ex:Set session for UserId) > UserSession.UserId = 1;
Get session (int id = UserSession.UserId)
- 3 回答
- 0 关注
- 154 浏览
添加回答
举报
