How to use Session thru Login, Authorization,Authentication?









up vote
0
down vote

favorite












First of all i am a newbie when it comes to programming and i am only self-learning and would like to ask for solutions that i have encountered and can't be solved by my own



I Can't make it work. i would like Session["userlevel"] == "Admin" to be the only one to access the view how can i make this to work please help..



public ActionResult Authorize(Test user)
{

using (TableEntities db = new TableEntities())
{

var loginUserAdmin = db.Employees.Where(x => x.Username == user.Username && x.UserPassword == user.UserPassword && x.AccountStatus == "Active" && x.AccountType == "Admin").FirstOrDefault();

if (loginUserAdmin == null || loginUserAdmin != loginUserAdmin)

else

ViewBag.Messageko = "Admin";
Session["userid"] = loginUserAdmin.EmployeeID;
Session["firstName"] = loginUserAdmin.FirstName;
Session["lastName"] = loginUserAdmin.LastName;
Session["userName"] = loginUserAdmin.Username;
Session["userPassword"] = loginUserAdmin.UserPassword;
Session["userlevel"] = loginUserAdmin.AccountType;
ViewBag.acc = loginUserAdmin.AccountType;





return RedirectToAction("Dashboard", "Test" ,"Login");




this is my Dashboard Controller



 [HttpGet]

public ActionResult Dashboard()

TableEntities db = new TableEntities();
ViewBag.Active = db.Employees.Where(x => x.AccountStatus == "Active");
ViewBag.Message = "GG";
return View();



And this is my View



@
ViewBag.Title = "Dashboard";
Layout = "~/Views/Shared/_Layout.cshtml";

@if (Session["userlevel"].ToString() == "Admin")


Response.Redirect("~/Test/Login");

else

if (ViewBag.Active != null)
<label style="font-size:12px; color:red;">@ViewBag.Messageko</label>
<table class="table table-striped">
<tr>
<th>EmployeeID</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>User Password</th>
<th>Account Status </th>
</tr>
@foreach (var User in ViewBag.Active)

<tr>
<td align="center">@User.EmployeeID</td>
<td align="center">@User.FirstName</td>
<td align="center">@User.MiddleName</td>
<td align="center">@User.LastName</td>
<td align="center">@User.Username</td>
<td align="center">@User.UserPassword</td>
<td align="center" class="text-success">@User.AccountStatus</td>
</tr>


</table>





Thank you Very Much










share|improve this question





















  • Please could somebody help me through this problem..
    – Clarence Baluyot
    Nov 15 at 6:57














up vote
0
down vote

favorite












First of all i am a newbie when it comes to programming and i am only self-learning and would like to ask for solutions that i have encountered and can't be solved by my own



I Can't make it work. i would like Session["userlevel"] == "Admin" to be the only one to access the view how can i make this to work please help..



public ActionResult Authorize(Test user)
{

using (TableEntities db = new TableEntities())
{

var loginUserAdmin = db.Employees.Where(x => x.Username == user.Username && x.UserPassword == user.UserPassword && x.AccountStatus == "Active" && x.AccountType == "Admin").FirstOrDefault();

if (loginUserAdmin == null || loginUserAdmin != loginUserAdmin)

else

ViewBag.Messageko = "Admin";
Session["userid"] = loginUserAdmin.EmployeeID;
Session["firstName"] = loginUserAdmin.FirstName;
Session["lastName"] = loginUserAdmin.LastName;
Session["userName"] = loginUserAdmin.Username;
Session["userPassword"] = loginUserAdmin.UserPassword;
Session["userlevel"] = loginUserAdmin.AccountType;
ViewBag.acc = loginUserAdmin.AccountType;





return RedirectToAction("Dashboard", "Test" ,"Login");




this is my Dashboard Controller



 [HttpGet]

public ActionResult Dashboard()

TableEntities db = new TableEntities();
ViewBag.Active = db.Employees.Where(x => x.AccountStatus == "Active");
ViewBag.Message = "GG";
return View();



And this is my View



@
ViewBag.Title = "Dashboard";
Layout = "~/Views/Shared/_Layout.cshtml";

@if (Session["userlevel"].ToString() == "Admin")


Response.Redirect("~/Test/Login");

else

if (ViewBag.Active != null)
<label style="font-size:12px; color:red;">@ViewBag.Messageko</label>
<table class="table table-striped">
<tr>
<th>EmployeeID</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>User Password</th>
<th>Account Status </th>
</tr>
@foreach (var User in ViewBag.Active)

<tr>
<td align="center">@User.EmployeeID</td>
<td align="center">@User.FirstName</td>
<td align="center">@User.MiddleName</td>
<td align="center">@User.LastName</td>
<td align="center">@User.Username</td>
<td align="center">@User.UserPassword</td>
<td align="center" class="text-success">@User.AccountStatus</td>
</tr>


</table>





Thank you Very Much










share|improve this question





















  • Please could somebody help me through this problem..
    – Clarence Baluyot
    Nov 15 at 6:57












up vote
0
down vote

favorite









up vote
0
down vote

favorite











First of all i am a newbie when it comes to programming and i am only self-learning and would like to ask for solutions that i have encountered and can't be solved by my own



I Can't make it work. i would like Session["userlevel"] == "Admin" to be the only one to access the view how can i make this to work please help..



public ActionResult Authorize(Test user)
{

using (TableEntities db = new TableEntities())
{

var loginUserAdmin = db.Employees.Where(x => x.Username == user.Username && x.UserPassword == user.UserPassword && x.AccountStatus == "Active" && x.AccountType == "Admin").FirstOrDefault();

if (loginUserAdmin == null || loginUserAdmin != loginUserAdmin)

else

ViewBag.Messageko = "Admin";
Session["userid"] = loginUserAdmin.EmployeeID;
Session["firstName"] = loginUserAdmin.FirstName;
Session["lastName"] = loginUserAdmin.LastName;
Session["userName"] = loginUserAdmin.Username;
Session["userPassword"] = loginUserAdmin.UserPassword;
Session["userlevel"] = loginUserAdmin.AccountType;
ViewBag.acc = loginUserAdmin.AccountType;





return RedirectToAction("Dashboard", "Test" ,"Login");




this is my Dashboard Controller



 [HttpGet]

public ActionResult Dashboard()

TableEntities db = new TableEntities();
ViewBag.Active = db.Employees.Where(x => x.AccountStatus == "Active");
ViewBag.Message = "GG";
return View();



And this is my View



@
ViewBag.Title = "Dashboard";
Layout = "~/Views/Shared/_Layout.cshtml";

@if (Session["userlevel"].ToString() == "Admin")


Response.Redirect("~/Test/Login");

else

if (ViewBag.Active != null)
<label style="font-size:12px; color:red;">@ViewBag.Messageko</label>
<table class="table table-striped">
<tr>
<th>EmployeeID</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>User Password</th>
<th>Account Status </th>
</tr>
@foreach (var User in ViewBag.Active)

<tr>
<td align="center">@User.EmployeeID</td>
<td align="center">@User.FirstName</td>
<td align="center">@User.MiddleName</td>
<td align="center">@User.LastName</td>
<td align="center">@User.Username</td>
<td align="center">@User.UserPassword</td>
<td align="center" class="text-success">@User.AccountStatus</td>
</tr>


</table>





Thank you Very Much










share|improve this question













First of all i am a newbie when it comes to programming and i am only self-learning and would like to ask for solutions that i have encountered and can't be solved by my own



I Can't make it work. i would like Session["userlevel"] == "Admin" to be the only one to access the view how can i make this to work please help..



public ActionResult Authorize(Test user)
{

using (TableEntities db = new TableEntities())
{

var loginUserAdmin = db.Employees.Where(x => x.Username == user.Username && x.UserPassword == user.UserPassword && x.AccountStatus == "Active" && x.AccountType == "Admin").FirstOrDefault();

if (loginUserAdmin == null || loginUserAdmin != loginUserAdmin)

else

ViewBag.Messageko = "Admin";
Session["userid"] = loginUserAdmin.EmployeeID;
Session["firstName"] = loginUserAdmin.FirstName;
Session["lastName"] = loginUserAdmin.LastName;
Session["userName"] = loginUserAdmin.Username;
Session["userPassword"] = loginUserAdmin.UserPassword;
Session["userlevel"] = loginUserAdmin.AccountType;
ViewBag.acc = loginUserAdmin.AccountType;





return RedirectToAction("Dashboard", "Test" ,"Login");




this is my Dashboard Controller



 [HttpGet]

public ActionResult Dashboard()

TableEntities db = new TableEntities();
ViewBag.Active = db.Employees.Where(x => x.AccountStatus == "Active");
ViewBag.Message = "GG";
return View();



And this is my View



@
ViewBag.Title = "Dashboard";
Layout = "~/Views/Shared/_Layout.cshtml";

@if (Session["userlevel"].ToString() == "Admin")


Response.Redirect("~/Test/Login");

else

if (ViewBag.Active != null)
<label style="font-size:12px; color:red;">@ViewBag.Messageko</label>
<table class="table table-striped">
<tr>
<th>EmployeeID</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>User Password</th>
<th>Account Status </th>
</tr>
@foreach (var User in ViewBag.Active)

<tr>
<td align="center">@User.EmployeeID</td>
<td align="center">@User.FirstName</td>
<td align="center">@User.MiddleName</td>
<td align="center">@User.LastName</td>
<td align="center">@User.Username</td>
<td align="center">@User.UserPassword</td>
<td align="center" class="text-success">@User.AccountStatus</td>
</tr>


</table>





Thank you Very Much







asp.net-mvc-5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 13:12









Clarence Baluyot

11




11











  • Please could somebody help me through this problem..
    – Clarence Baluyot
    Nov 15 at 6:57
















  • Please could somebody help me through this problem..
    – Clarence Baluyot
    Nov 15 at 6:57















Please could somebody help me through this problem..
– Clarence Baluyot
Nov 15 at 6:57




Please could somebody help me through this problem..
– Clarence Baluyot
Nov 15 at 6:57












1 Answer
1






active

oldest

votes

















up vote
0
down vote













In Visual Studio 2015 @if (Session["userlevel"].ToString() == "Admin") works!!
But in 2017 i dont know but



@if (Session["userlevel"].ToString().Trim() == "Admin") works!!






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249074%2fhow-to-use-session-thru-login-authorization-authentication%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    In Visual Studio 2015 @if (Session["userlevel"].ToString() == "Admin") works!!
    But in 2017 i dont know but



    @if (Session["userlevel"].ToString().Trim() == "Admin") works!!






    share|improve this answer
























      up vote
      0
      down vote













      In Visual Studio 2015 @if (Session["userlevel"].ToString() == "Admin") works!!
      But in 2017 i dont know but



      @if (Session["userlevel"].ToString().Trim() == "Admin") works!!






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        In Visual Studio 2015 @if (Session["userlevel"].ToString() == "Admin") works!!
        But in 2017 i dont know but



        @if (Session["userlevel"].ToString().Trim() == "Admin") works!!






        share|improve this answer












        In Visual Studio 2015 @if (Session["userlevel"].ToString() == "Admin") works!!
        But in 2017 i dont know but



        @if (Session["userlevel"].ToString().Trim() == "Admin") works!!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 at 5:02









        Clarence Baluyot

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249074%2fhow-to-use-session-thru-login-authorization-authentication%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto