如何在 C# 页面 ASP.NET Core MVC 上使用 SignInManager 和 Usermanager

Posted

技术标签:

【中文标题】如何在 C# 页面 ASP.NET Core MVC 上使用 SignInManager 和 Usermanager【英文标题】:How to use SignIn Manager and User Manager on C# pages ASP.NET Core MVC 【发布时间】:2021-11-22 02:07:17 【问题描述】:

我正在尝试通过让登录用户键入一个数字来更新数据库,然后它会使用登录管理器和用户管理器从数据库中添加该数字。我希望它获取存储在数据库中的登录用户帐户余额并将其添加到他们键入的数字中。然后我想用组合数字更新数据库。

但是,我不知道如何让控制器使用登录管理器。有了剃须刀页面,我所要做的就是使用 @inject SignInManager<CardUs> SignInManager 并使用 @inject UserManager<CardUs> UserManager 并导入 Microsoft.AspNetCore.Identity 但是当我尝试使用UserManager.GetUserAsync(User).Result.AccountBalance 时会抛出很多错误。

其中一个是:

功能“***语句”在 C# 8.0 中不可用。请使用语言版本 9.0 或更高版本

所以我从 8.0 更新到 9.0,但那没有用。现在是说

在此上下文中,不能使用在***语句中声明的局部变量或局部函数“SigninManager”

我尝试查看 Microsoft 页面,但没有任何结果。这是错误代码 CS8801。

如何在控制器中使用登录管理器和用户管理器变量?

这个项目没有完成。我还没有将它发送到数据库。它所做的一切都是接受用户输入并将其显示在下一个屏幕上

代码:

帐户控制器:

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();
        
    

存款.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>

存款模型:

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; 
    

这是登录管理器在剃须刀页面上的外观:

@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: $@UserManager.GetUserAsync(User).Result.AccountBalance </div>
    </div>
</div>

【问题讨论】:

【参考方案1】:
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();
        
    

【讨论】:

我也试过这样做。它只是抛出了 CS0116 错误,“命名空间不能直接包含字段或方法等成员。”我不得不将它包装在一个类中,所以 public class ManagerSignInManager SignInManager;用户管理器 用户管理器; 。然后我在 deposit HttpPost 方法中从管理器类中创建了一个对象。但这没有用,它只会引发更多错误 我还注意到我有 SignInManager.GetUserAsync(User).Result.AccountBalance 而不是 @UserManager.GetUserAsync(User).Result.AccountBalance 请考虑添加how and why this solves the problem的简要说明。这将有助于读者更好地理解您的解决方案。【参考方案2】:

我想通了。我设置错了。我必须将它添加到构造函数中,并且我必须创建和分配字段。

代码:

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();
        
    

【讨论】:

以上是关于如何在 C# 页面 ASP.NET Core MVC 上使用 SignInManager 和 Usermanager的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 ASP.NET Core 同时制作 Razor Page 和 C# 代码框架?

c# asp.net GridView中如何删除一条记录后刷新页面

Blazor——Asp.net core的新前端框架

如何在 C# 中将数据发送到 ASP.NET Core MVC 控制器?

如何使用 C# 在 ASP.NET Core 3.1 MVC 中使用会话变量

如何在 Asp.NET Core WEB API 中使用 .Net (C#) 在 Payload 中创建具有自定义 JSON 声明的 JWT 令牌