You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASP.NET Core 6 MVC首页加载Identity登录页面

解决ASP.NET Core 6 MVC嵌入Identity登录视图的问题

核心问题原因

直接调用Identity区域的Login.cshtml视图触发InvalidOperation异常,本质是因为Identity登录视图依赖Identity核心服务(如SignInManager、UserManager)、防伪造令牌验证机制,以及默认的Identity布局上下文,单独加载视图会缺失这些必要依赖项。

可行解决方案

方案1:封装独立Partial View(推荐生产环境使用)

  1. 在Views/Shared目录下创建_LoginPartial.cshtml,迁移登录表单核心逻辑:
    @using Microsoft.AspNetCore.Identity
    @inject SignInManager<IdentityUser> SignInManager
    @inject UserManager<IdentityUser> UserManager
    
    @if (SignInManager.IsSignedIn(User))
    {
        <form asp-area="Identity" asp-page="/Account/Logout" method="post" class="text-right">
            <button type="submit" class="btn btn-link text-dark">退出登录 @User.Identity?.Name</button>
        </form>
    }
    else
    {
        <div class="login-panel">
            <form asp-area="Identity" asp-page="/Account/Login" method="post">
                <div asp-validation-summary="All" class="text-danger small mb-2"></div>
                <div class="mb-2">
                    <label asp-for="Input.Email" class="form-label small"></label>
                    <input asp-for="Input.Email" class="form-control form-control-sm" />
                    <span asp-validation-for="Input.Email" class="text-danger small"></span>
                </div>
                <div class="mb-2">
                    <label asp-for="Input.Password" class="form-label small"></label>
                    <input asp-for="Input.Password" class="form-control form-control-sm" />
                    <span asp-validation-for="Input.Password" class="text-danger small"></span>
                </div>
                <div class="mb-2">
                    <label asp-for="Input.RememberMe" class="form-check-label small">
                        <input asp-for="Input.RememberMe" class="form-check-input" />
                        @Html.DisplayNameFor(m => m.RememberMe)
                    </label>
                </div>
                <button type="submit" class="btn btn-sm btn-primary w-100">登录</button>
            </form>
        </div>
    }
    
  2. 在首页(如Index.cshtml)右侧区域直接引用该Partial:
    <div class="col-md-3">
        <partial name="_LoginPartial" />
    </div>
    

方案2:iframe嵌入登录页面(仅适合快速测试)

如果不想重构视图,可临时用iframe嵌入登录页面,注意处理登录后的跳转逻辑:

<div class="col-md-3">
   <iframe src="/Identity/Account/Login" width="100%" height="420px" frameborder="0" scrolling="no"></iframe>
</div>

注:该方式存在会话上下文、跨域样式冲突等潜在问题,不建议生产环境使用。

方案3:控制器中转加载Identity视图

  1. 在HomeController中添加动作方法,注入Identity服务并返回登录视图:
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Mvc;
    
    public class HomeController : Controller
    {
        private readonly SignInManager<IdentityUser> _signInManager;
    
        public HomeController(SignInManager<IdentityUser> signInManager)
        {
            _signInManager = signInManager;
        }
    
        public IActionResult EmbeddedLogin()
        {
            // 确保视图能获取到Identity服务上下文
            ViewData["SignInManager"] = _signInManager;
            return View("/Areas/Identity/Pages/Account/Login.cshtml");
        }
    }
    
  2. 在首页调用该动作:
    <div class="col-md-3">
        @Html.Action("EmbeddedLogin", "Home")
    </div>
    

关键排查点

  • 检查Program.cs是否正确注册Identity服务:确保包含builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<ApplicationDbContext>();
  • 确认视图或控制器已正确注入SignInManager和UserManager,避免服务未注册的异常
  • 检查Identity视图所在目录的_ViewImports.cshtml,是否包含@using Microsoft.AspNetCore.Identity命名空间引用

内容的提问来源于stack exchange,提问作者mark Iltzsch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:05:47