为什么 HttpContext.User.Identity.Name 不定期为空?

发布于 2025-01-11 17:07:28 字数 1023 浏览 0 评论 0原文

context.User.Identity.Name 很少为空。我认为这是不可能的,因为我在使用用户之前检查用户是否经过身份验证,如果没有,则重定向到登录页面。 99% 的情况下,此代码运行良好,但有时在尝试 GetMemberNumber 时会遇到问题,因为传递的值是空的。有谁知道这里会发生什么吗?

FromRA.ashx.cs

using System;
using System.Threading.Tasks;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using Helpers;

namespace Transfer
{
   public class FromRA : HttpTaskAsyncHandler
   {
       public override async Task ProcessRequestAsync(HttpContext context)
       {
           if (!context.User.Identity.IsAuthenticated)
               FormsAuthentication.RedirectToLoginPage();

           var memberNumber = MembershipUtils.GetMemberNumber(context.User.Identity.Name);

           var remediationServiceProviderResult = await Global.RestrictedAccess
               .GetRemediationServiceProviderResult(memberNumber);

           //...irrelevant code
       }
   }
}

Web.config

context.User.Identity.Name will very rarely be empty. I did not think this was possible since I'm checking if the user is authenticated before using it, and redirecting to the login page if they are not. 99% of the time this code runs fine, but I'm sometimes having issues when trying to GetMemberNumber, as the value that is being passed is a empty. Does anyone have any ideas what could be going on here?

FromRA.ashx.cs

using System;
using System.Threading.Tasks;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using Helpers;

namespace Transfer
{
   public class FromRA : HttpTaskAsyncHandler
   {
       public override async Task ProcessRequestAsync(HttpContext context)
       {
           if (!context.User.Identity.IsAuthenticated)
               FormsAuthentication.RedirectToLoginPage();

           var memberNumber = MembershipUtils.GetMemberNumber(context.User.Identity.Name);

           var remediationServiceProviderResult = await Global.RestrictedAccess
               .GetRemediationServiceProviderResult(memberNumber);

           //...irrelevant code
       }
   }
}

Web.config

<authentication mode="Forms">

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2025-01-18 17:07:29

重定向后代码执行不会停止。

    public override async Task ProcessRequestAsync(HttpContext context)
           {
               if (!context.User.Identity.IsAuthenticated)
               {
                   FormsAuthentication.RedirectToLoginPage();
                   return;
               }
    
    
               var memberNumber = MembershipUtils.GetMemberNumber(context.User.Identity.Name);
    
               var remediationServiceProviderResult = await Global.RestrictedAccess
                   .GetRemediationServiceProviderResult(memberNumber);
    
               //...irrelevant code
           }

code execution will not stop after redirect.

    public override async Task ProcessRequestAsync(HttpContext context)
           {
               if (!context.User.Identity.IsAuthenticated)
               {
                   FormsAuthentication.RedirectToLoginPage();
                   return;
               }
    
    
               var memberNumber = MembershipUtils.GetMemberNumber(context.User.Identity.Name);
    
               var remediationServiceProviderResult = await Global.RestrictedAccess
                   .GetRemediationServiceProviderResult(memberNumber);
    
               //...irrelevant code
           }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文