In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to get the current login user information in ASP.NET Core 2.0 MVC, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Use Session to store the currently logged-in user information
First, to use Session in ASP.NET Core, we need to inject Session into the pipeline of ASP.NET Core, in the same way we use MVC, in ConfigureServices (IServiceCollection services), add
one
Services.AddSession ()
Add to Configure (IApplicationBuilder app, IHostingEnvironment env)
one
App.UseSession ()
This way, we can use Session in MVC. Of course, now we can only get the Session object in Controller. If we want to use the Session object in other class files, we need to inject the IHttpContextAccessor object. Here, we can use nuget to add the Microsoft.AspNetCore.Http.Extensions assembly, which makes it easy for us to operate on Session.
Because we use the static class as the carrier for the currently logged-in user, and the static class cannot have an instance constructor, I use a configuration method to inject it, as shown in the CurrentUser class.
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
forty-four
forty-five
forty-six
forty-seven
forty-eight
forty-nine
fifty
fifty-one
fifty-two
fifty-three
fifty-four
fifty-five
fifty-six
fifty-seven
fifty-eight
fifty-nine
sixty
sixty-one
sixty-two
sixty-three
sixty-four
sixty-five
sixty-six
sixty-seven
sixty-eight
sixty-nine
seventy
seventy-one
seventy-two
seventy-three
seventy-four
seventy-five
seventy-six
seventy-seven
seventy-eight
seventy-nine
eighty
eighty-one
eighty-two
Public static class CurrentUser
{
# region Initialize
Private static IHttpContextAccessor _ httpContextAccessor
Private static ISession _ session = > _ httpContextAccessor.HttpContext.Session
Public static void Configure (IHttpContextAccessor httpContextAccessor)
{
_ httpContextAccessor = httpContextAccessor
}
# endregion
# region Attribute
/ / /
/ / user primary key
/ / /
Public static string UserOID
{
Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserOID")
Set = > _ session.SetString (CurrentUser_UserOID,! string.IsNullOrEmpty (value)? Value: "")
}
/ / /
/ / user number
/ / /
Public static long UserId
{
Get = > _ session = = null? 0: Convert.ToInt64 (_ session.GetString ("CurrentUser_UserId"))
Set = > _ session.SetString ("CurrentUser_UserId", value! = 0? Value.ToString (): "0")
}
/ / /
/ / user name
/ / /
Public static string UserName
{
Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserName")
Set = > _ session.SetString (CurrentUser_UserName,! string.IsNullOrEmpty (value)? Value: "")
}
/ / /
/ / user logs in to the account
/ / /
Public static string UserAccount
{
Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserAccount")
Set = > _ session.SetString (CurrentUser_UserAccount,! string.IsNullOrEmpty (value)? Value: "")
}
/ / /
/ / user avatar address
/ / /
Public static string UserImage
{
Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserImage")
Set = > _ session.SetString (CurrentUser_UserImage,! string.IsNullOrEmpty (value)? Value: "")
}
/ / /
/ / user role
/ / /
Public static string UserRole
{
Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserRole")
Set = > _ session.SetString (CurrentUser_UserRole,! string.IsNullOrEmpty (value)? Value: "")
}
/ / /
/ / Home address
/ / /
Public static string UserPage
{
Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserPage")
Set = > _ session.SetString (CurrentUser_UserPage,! string.IsNullOrEmpty (value)? Value: "")
}
# endregion
}
When we have created such a static class, we can assign the currently logged-in user information to the static class after the login is successful, so that we can directly use the static class CurrentUser where we need it. After the actual use at that time, it is found that if you want to get the user information stored after login, you must call the Configure method of CurrentUser in the construction method of Controller, which virtually increases a lot of work.
The sample code for the constructor of Controller is as follows:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
[Area ("Administrator")]
[Authorize (Policy = "Administrator")]
Public class HomeController: DanvicController
{
# region Initialize
Private readonly ApplicationDbContext _ context
Private readonly ILogger _ logger
Private readonly IHomeService _ service
Private readonly IHttpContextAccessor _ httpContextAccessor
Public HomeController (IHomeService service, ILogger logger, IHttpContextAccessor httpContextAccessor, ApplicationDbContext context)
{
_ service = service
_ logger = logger
_ httpContextAccessor = httpContextAccessor
_ context = context
CurrentUser.Configure (_ httpContextAccessor)
}
# endregion
# region View
# endregion
}
The relevant code for assigning a value to CurrentUser after a successful login is as follows:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
/ / /
/ set the currently logged in user
/ / /
Public async Task SetCurrentUser (string oid, IHttpContextAccessor httpContextAccessor, ApplicationDbContext context)
{
CurrentUser.Configure (httpContextAccessor)
Var user = await PSURepository.GetUserByOIDAsync (oid, context)
If (user! = null)
{
String role = string.Empty
Switch (user.AccountType)
{
Case 0:
Role = "Administrator"
Break
Case 1:
Role = "Instructor"
Break
Case 2:
Role = "Student"
Break
}
CurrentUser.UserAccount = user.Account
CurrentUser.UserId = user.Id
CurrentUser.UserImage = user.ImageSrc
CurrentUser.UserName = user.Name
CurrentUser.UserOID = user.IdentityUserOID
CurrentUser.UserRole = role
CurrentUser.UserPage = user.HomePage
}
}
So much for sharing about how to get the current login user information in ASP.NET Core 2.0 MVC. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.