Answered by:
how can I get the value of AspNetUsers column value from another controller

Question
-
User-1355965324 posted
Hi
I have added one additional column to the existing AspNetUsers . The column name IsAuthorised . Some user have the value 0 and some users have the value 1 against this column. How can I get the value in controller to get the login user is with IsAuthorised = true or false. How can I check in my cntroller please help
namespace MetaWeather.Areas.Customer.Controllers { [Area("Customer")] [Authorize] public class ForcastController : Controller { private readonly IWeatherRepository _weatherRepository; private readonly IUnitOfWork _unitOfWork; public Weather Weather { get; set; } public ForcastController(IWeatherRepository weatherRepository, IUnitOfWork unitOfWork) { _weatherRepository = weatherRepository; _unitOfWork = unitOfWork; } public async Task<IActionResult> Index() { bool isAuthorisdUser = //how can I get the value from login user IsAuthorised }
public class InputModel { [Required] [EmailAddress] [Display(Name = "Email")] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } [Required] public string Name { get; set; } public string StreetAddress { get; set; } public string City { get; set; } public string State { get; set; } public string PostalCode { get; set; } public bool IsAuthorised { get; set; } }
Thursday, June 4, 2020 7:07 PM
Answers
-
User475983607 posted
polachan
I have added one additional column to the existing AspNetUsers . The column name IsAuthorised . Some user have the value 0 and some users have the value 1 against this column. How can I get the value in controller to get the login user is with IsAuthorised = true or false. How can I check in my cntroller please helpYou're asking this question as if the community knows how you security is designed. If I assume you are using ASP.NET Core Identity API, then the solution is very simple. Simply get the user using the UserManager.
private readonly ILogger<IndexModel> _logger; private readonly UserManager<IdentityUser> _userManager; public TheConstructor (ILogger<IndexModel> logger, UserManager<IdentityUser> userManager) { _logger = logger; _userManager = userManager; }
var user = await _userManager.GetUserAsync(User);
var IsAuthorised = user.IsAuthorised;If you've build custom security API then you are the only person that knows how the security works. There is no way for the community to provide a solution.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 4, 2020 7:31 PM
All replies
-
User475983607 posted
polachan
I have added one additional column to the existing AspNetUsers . The column name IsAuthorised . Some user have the value 0 and some users have the value 1 against this column. How can I get the value in controller to get the login user is with IsAuthorised = true or false. How can I check in my cntroller please helpYou're asking this question as if the community knows how you security is designed. If I assume you are using ASP.NET Core Identity API, then the solution is very simple. Simply get the user using the UserManager.
private readonly ILogger<IndexModel> _logger; private readonly UserManager<IdentityUser> _userManager; public TheConstructor (ILogger<IndexModel> logger, UserManager<IdentityUser> userManager) { _logger = logger; _userManager = userManager; }
var user = await _userManager.GetUserAsync(User);
var IsAuthorised = user.IsAuthorised;If you've build custom security API then you are the only person that knows how the security works. There is no way for the community to provide a solution.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, June 4, 2020 7:31 PM -
User-1355965324 posted
Many Many Thanks
Thursday, June 4, 2020 7:58 PM