为什么httpcontext.user.isterity.name包含错误的名称?

发布于 2025-01-20 22:04:18 字数 2070 浏览 0 评论 0原文

我有一个.NET 5 MVC应用程序,该应用程序会定期检查基于httpcontext.user.identity.name值的访问。该应用使用Azure AD。有时,当我在用户之间切换时(登录并签署)时,httpcontext.user.identity.name值将包含先前的用户名称,当前用户将访问以前的用户。

更奇怪的是显示的登录是正确的。

这只是Visual Studio生成的锅炉板代码:

@using System.Security.Principal

<ul class="navbar-nav">
@if (User.Identity.IsAuthenticated)
{
    <li class="nav-item">
        <span class="navbar-text text-light">Hello @User.Identity.Name!</span>
    </li>
    <li class="nav-item">
        <a class="nav-link text-light" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Sign out</a>
    </li>
}
else
{
    <li class="nav-item">
        <a class="nav-link text-light" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn">Sign in</a>
    </li>
}
</ul>

so @user.identity.name .cshtml上总是正确的,但是httpcontext.user.user.inderity.name在控制器上是先前的(缓存? )用户。

有什么线索,为什么以前的用户被缓存?

这是我在控制器上称呼它的示例:

ViewData["User"] = HttpContext.User;
ViewData["Menu"] = await _menuService.GetReportMenuAsync(MenuService.ReportMenuItemOption.Genesis01, User.Identity.Name);
var userAccess = await _userAccessService.GetUserEntityAccessAsync(User.Identity.Name);
            var model = new SomeModel
            {
                SearchDate = DateTime.Now.AddDays(-7),
                Banks = userAccess.Banks,
                ATMIds = userAccess.ATMs
            };                

            return View(model);

这是在我的创业公司中:

 services.AddControllersWithViews(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        }).AddMicrosoftIdentityUI();

I have a .NET 5 MVC application that periodically checks access based on the HttpContext.User.Identity.Name value. The app uses Azure AD. Sometimes as I switch between users (Signing out and Signing in), the HttpContext.User.Identity.Name value will contain the previous user's name and the current user will have the access of the previous user.

Even more strange is the the login shown is correct.

login name

This is just the boiler plate code generated by visual studio:

@using System.Security.Principal

<ul class="navbar-nav">
@if (User.Identity.IsAuthenticated)
{
    <li class="nav-item">
        <span class="navbar-text text-light">Hello @User.Identity.Name!</span>
    </li>
    <li class="nav-item">
        <a class="nav-link text-light" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Sign out</a>
    </li>
}
else
{
    <li class="nav-item">
        <a class="nav-link text-light" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignIn">Sign in</a>
    </li>
}
</ul>

So @User.Identity.Name on the .cshtml is always correct, but HttpContext.User.Identity.Name on the controller is the previous (cached?) user.

Any clues to why the previous user gets cached?

Here is a sample of how I call it on the controller:

ViewData["User"] = HttpContext.User;
ViewData["Menu"] = await _menuService.GetReportMenuAsync(MenuService.ReportMenuItemOption.Genesis01, User.Identity.Name);
var userAccess = await _userAccessService.GetUserEntityAccessAsync(User.Identity.Name);
            var model = new SomeModel
            {
                SearchDate = DateTime.Now.AddDays(-7),
                Banks = userAccess.Banks,
                ATMIds = userAccess.ATMs
            };                

            return View(model);

This is in my StartUp:

 services.AddControllersWithViews(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        }).AddMicrosoftIdentityUI();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文