我有一个控制器,其中有几种方法。如果登录成功,则在登录方法中,我启动HttpContext.Session.SetInt32("ID")
。此ID是已登录用户的ID。我在许多其他方法中使用此ID进行更多的计算和查询。在每种方法中,我都必须声明一个变量,例如
?int userId = HttpContext.Session.GetINT32("ID")
在该方法中使用此变量。
如何在不使用HttpContext.Session.GetInt32
语句的情况下,以可以在其他Action方法中使用它的方式全局声明它。我只是尝试使用DRY原理,以使我的代码更简洁。
有什么帮助吗?
答案 0 :(得分:2)
using AlhabtoorTennisAcademy.CustomFilters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace ProjectNameSpace
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// webforms page route
//Custom route code for webform
routes.MapPageRoute("home", "WebForm", "~/WebForm.aspx", false,
new RouteValueDictionary {
{ "path", "page-not-found" },{ "pagename", "page-not-found" }
});
}
}
........
答案 1 :(得分:1)
如果您使用的是Identity,则可以这样获得用户ID:
other_linker_flags
答案 2 :(得分:0)
在ASP.NET Core中(全球)管理会话的最佳方法是:
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);
}
}
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);
}
}
}
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>());
}
--------------------
How to use :
Set session (Ex:Set session for UserId) > UserSession.UserId = 1;
Get session (int id = UserSession.UserId)