如何直接在Razor中从ASP.NET身份获取UserId()方法 [英] How To Get UserId() Method From ASP.NET Identity Directly in Razor
问题描述
我在自定义授权"中遇到了此问题,该问题在我的 Views
的某些部分(我不想放在 PartialView
上),而我正在使用如果
语句如下:
I've this issue with Custom Authorization some parts of my Views
(which i don't want to put on PartialView
), instead I am using If
statement like below:
@if (item.CurrentComment.Id == Guid.Parse(ViewBag.UserId) || repository.IsUserInRoles(Guid.Parse(ViewBag.UserId),"Manager") )
{
<dd>
<a href="@Url.Action("EditComment", "Ticketing",new { id = item.CurrentComment.Id, ticketId = Model.Tickets.CurrentTicket.Id })" class="btn btn-primary btn-sm" data-toggle="tooltip" data-placement="left">Edit</a>
</dd>
<dd>
<a href="@Url.Action("RemoveComment", "Ticketing",new { id = item.CurrentComment.Id, ticketId = Model.Tickets.CurrentTicket.Id })" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="left">Remove</a>
</dd>
}
此刻,我在控制器中获取了 UserId
,并将其传递给了如下所示的ViewBag:
At the moment I get my UserId
in my controller and pass it over with a ViewBag like below:
ViewBag.UserId = User.Identity.GetUserId();
我想知道如何在剃刀中直接调用 GetUserId
方法,这样我就不再使用ViewBag发送它了.我试过使用 User.Identity.
,但是那里的Method未知,我认为它不知道扩展名.有办法解决吗?
I wondered how to call the GetUserId
method directly in a razor so that I won't send it with a ViewBag anymore. I've tried using User.Identity.
but the Method is Unknown there, which I assume it doesn't know the extension. Is there a way around it?
推荐答案
在视图文件顶部添加以下 using
语句.
Add the following using
statement at the top of your view file.
使用Microsoft.AspNet.Identity;
在此之后,您应该可以使用 User.Identity.GetUserId()
Following this, you should be able to use
User.Identity.GetUserId()
这篇关于如何直接在Razor中从ASP.NET身份获取UserId()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!