locked
store a query result in HttpContext.Session and handle it to view RRS feed

  • Question

  • User1051638994 posted

    Hello I need to store a query result in HttpContext.Session and handle it to razor view

    Here is what I have try so far

     var role = from u in _context.Users
                           join a in _context.Assignments
                           on u.UserID equals a.UserID
                           where u.UserName == User.Identity.Name
                           select u.RoleID;
     HttpContext.Session.SetString("UserRole", role.ToString());

    and in my view

     @if (HttpContextAccessor.HttpContext.Session.GetInt32("UserRole") == 1)
        {
     
            <div class="form-group col-4">
                <select asp-for="UserID" class="form-control" asp-items="ViewBag.UserID"></select>
            </div>
    
        }

    What I am try is to hide or show the dropdown depend on roleID. Any Idea? thank you

    Friday, November 6, 2020 7:06 AM

Answers

  • User1312693872 posted

    Hi,mspace

    You should add something in startup.cs, please check my demo:

    And you store string data in Session but you use int in view, you should make them the same.

    Configure:

    app.UseSession();

    ConfigureServices:

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSession();

    Then in Controller:

    public IActionResult Index()
            {
                HttpContext.Session.SetInt32("UserRole", 1);
                return View();
            }

    View:

    @using Microsoft.AspNetCore.Http
    @inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
    
    @if (HttpContextAccessor.HttpContext.Session.GetInt32("UserRole")==1)
    {
        <div class="form-group col-4">
            OK
        </div>
    }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 6, 2020 9:03 AM

All replies

  • User1312693872 posted

    Hi,mspace

    You should add something in startup.cs, please check my demo:

    And you store string data in Session but you use int in view, you should make them the same.

    Configure:

    app.UseSession();

    ConfigureServices:

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSession();

    Then in Controller:

    public IActionResult Index()
            {
                HttpContext.Session.SetInt32("UserRole", 1);
                return View();
            }

    View:

    @using Microsoft.AspNetCore.Http
    @inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
    
    @if (HttpContextAccessor.HttpContext.Session.GetInt32("UserRole")==1)
    {
        <div class="form-group col-4">
            OK
        </div>
    }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 6, 2020 9:03 AM
  • User-474980206 posted

    this is poor design. roles should be stored as claims with the user, not in session. then the view can use use the authorization policies.

      https://docs.microsoft.com/en-us/aspnet/core/security/authorization/views?view=aspnetcore-3.1

    Friday, November 6, 2020 4:26 PM