asp.net mvc核心-等效于User.IsSignedIn() [英] asp.net mvc core - equivalent of User.IsSignedIn()

查看:71
本文介绍了asp.net mvc核心-等效于User.IsSignedIn()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Web应用程序迁移到新的asp.net core模型上,并且突然遇到其中一种观点.

I am migrating a web application over to the new asp.net core model, and have hit a sudden snag with one of the views.

在新模型下,我找不到与UserUser.IsSignedIn()等效的迁移对象-在视图中使用时,像这样...

I cannot find the migrated equivalent to User and User.IsSignedIn() under the new model - when used in views, like this...

@using System.Security.Claims
@if (User.IsSignedIn())
{
}

我尝试导入Microsoft.AspNetCore.Mvc.Razor库,我认为该库将保存在该库中,但是它似乎无法以这种方式工作.

I've tried importing the Microsoft.AspNetCore.Mvc.Razor library, where I thought it would be held, but it doesn't seem to work that way.

推荐答案

ASP.NET团队针对RC2采取的方法是使用SignInManager.IsSignedIn:

The approach adopted by the ASP.NET team for RC2 is to use SignInManager.IsSignedIn:

@using Microsoft.AspNetCore.Identity
@using Mvc.Server.Models

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@if (SignInManager.IsSignedIn(User)) {
    <form asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
            </li>
            <li>
                <button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button>
            </li>
        </ul>
    </form>
}
else {
    <ul class="nav navbar-nav navbar-right">
        <li><a asp-controller="Account" asp-action="Register">Register</a></li>
        <li><a asp-controller="Account" asp-action="Login">Log in</a></li>
    </ul>
}

这篇关于asp.net mvc核心-等效于User.IsSignedIn()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆