Home > OS >  Why is HttpContext.User.Identity.Name irregularly empty?
Why is HttpContext.User.Identity.Name irregularly empty?

Time:03-05

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">

CodePudding user response:

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
           }
  • Related