Home > database >  How to use SignIn Manager and User Manager on C# pages ASP.NET Core MVC
How to use SignIn Manager and User Manager on C# pages ASP.NET Core MVC

Time:11-22

I'm trying to update a database by having the signed in user type a number and it adds that number from the database using the signin manager and usermanager. I want it to take the signed in user's account balance that's stored in the database and add it to the number they typed. Then i want to update the database with the combined number.

However, I'm stuck on how I would get the controller to use the sign in manager. With the razor page all I have to do is use @inject SignInManager<CardUs> SignInManager and use @inject UserManager<CardUs> UserManager and also import the Microsoft.AspNetCore.Identity but when I try to use UserManager.GetUserAsync(User).Result.AccountBalance it throws a lot of errors.

One of them being:

Feature 'top-level statements' is not available in C# 8.0. Please use language version 9.0 or greater

So I updated from 8.0 to 9.0 but then that didn't work. Now it's saying

Cannot use local variable or local function 'SigninManager' declared in a top-level statement in this context

I tried looking at the Microsoft page but nothing came up. This is error code CS8801.

How do I use the signin manager and user manager variable inside the controller?

This project not finished. I haven't sent it to the database yet. All it does it take the user input and show it on the next screen

Code:

AccountController:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Card.Areas.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Models;

SignInManager<CardUs> SignInManager;
UserManager<CardUs> UserManager;

namespace Card.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        private readonly TransactionConnection _tc;

        public AccountController(TransactionConnection tc)
        {
            _tc = tc;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Transactions()
        {
            var results = _tc.TransactionHistory.ToList();
            return View(results);
        }

        [HttpGet]
        public IActionResult Deposit()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Deposit(DepositModel model)
        {
            int test = model.AccountBalance   UserManager.GetUserAsync(User).Result.AccountBalance;

            return Content($"this is how much you entered {test}");
        }

        public IActionResult Transfers()
        {
            return View();
        }
    }
}

Deposit.cshtml:

@model Card.Models.DepositModel

@{
    ViewData["Title"] = "Deposit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<form asp-action="Deposit" asp-controller="Account">
    <label asp-for="AccountBalance"></label>
    <input asp-for="AccountBalance" placeholder="How much do you want"/>
    <input type="submit"/>
</form>

DepositModel:

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Areas.Identity.Data;

namespace Card.Models
{
    public class DepositModel
    {
        public int AccountBalance { get; set; }
    }
}

And here's how the sign in manager look on a razor page:

@using Microsoft.AspNetCore.Identity
@using Card.Areas.Identity.Data

@inject SignInManager<CardUs> SignInManager
@inject UserManager<CardUs> UserManager
@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
<style>
    div.a {
        text-align: right;
    }

    h1 {
        text-align: right;
    }

    .left {
        text-align: left;
    }

    .right {
        text-align: right;
    }

    .left, .right {
        display: inline-block;
        width: 50%;
    }
</style>


<div class="alert alert-primary" role="alert">
    <div>
        <div class="left"><p1>Hello <strong>@UserManager.GetUserAsync(User).Result.FirstName</strong> welcome back!</p1></div><div class="right">account balance: [email protected](User).Result.AccountBalance </div>
    </div>
</div>

CodePudding user response:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Card.Areas.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Models;

namespace Card.Controllers {
    SignInManager < CardUs > SignInManager;
    UserManager < CardUs > UserManager;

    [Authorize]
    public class AccountController: Controller {
        private readonly TransactionConnection _tc;

        public AccountController(TransactionConnection tc) {
            _tc = tc;
        }
        public IActionResult Index() {
            return View();
        }
        public IActionResult Transactions() {
                var results = _tc.TransactionHistory.ToList();
                return View(results);
            }
            [HttpGet]
        public IActionResult Deposit() {
                return View();
            }
            [HttpPost]
        public IActionResult Deposit(DepositModel model) {
            int test = model.AccountBalance   SignInManager.GetUserAsync(User).Result.AccountBalance;

            return Content($"this is how much you entered {test}");
        }
        public IActionResult Transfers() {
            return View();
        }
    }
}

CodePudding user response:

I figured it out. I was setting it up wrong. I had to add it to the constructor, and I had to create and assign fields.

Code:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Card.Areas.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Models;






namespace Card.Controllers
{
   
       
   
    
    [Authorize]
    public class AccountController : Controller
    {
        private readonly TransactionConnection _tc;
        private readonly UserManager<CardUs> userManager;
        private readonly SignInManager<CardUs> signInManager;

        public AccountController(TransactionConnection tc, UserManager<CardUs> userManager, SignInManager<CardUs> signInManager)
        {
            _tc = tc;
            this.userManager = userManager;
            this.signInManager = signInManager;
        }
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult Transactions()
        {
            var results = _tc.TransactionHistory.ToList();
            return View(results);
        }
        [HttpGet]
        public IActionResult Deposit()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Deposit(DepositModel model)
        {
            
            int test = model.AccountBalance   userManager.GetUserAsync(User).Result.AccountBalance;

            return Content($"this is how much you entered {test}");
        }
        public IActionResult Transfers()
        {
            return View();
        }
    }
}
  • Related