Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.05/5 (4 votes)
I am trying to authenticate Anonymously in my mvc 5 project, it seems to work fine in my local host, but after deploying to production server, it does not see see the anonymous authentication tag in web.config file, i am having this error: Object reference not set to instance of an object, meaning the Request.Anonymous field is null, i dont know how to get this work in mvc 5 e-commerce site on production, i will appreciate any help. Thanks.

Here is my code: I added this line of code to the config file inside system.web:

XML
<anonymousIdentification enabled="true" cookieless="UseCookies" cookieName=".ASPXANONYMOUS" cookieTimeout="4320" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" /> 


And this is how i reference it in my code behind:

C#
var getRecord = (from u in db.ShoppingCarts.Where(x => x.CartSessionID.Equals(Request.AnonymousID)) select u).ToList();


What I have tried:

Here is my code: I added this line of code to the config file inside system.web:

XML
<anonymousIdentification enabled="true" cookieless="UseCookies" cookieName=".ASPXANONYMOUS" cookieTimeout="4320" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" /> 


And this is how i reference it in my code behind:

C#
var getRecord = (from u in db.ShoppingCarts.Where(x => x.CartSessionID.Equals(Request.AnonymousID)) select u).ToList();
Posted
Updated 2-Mar-23 22:16pm

1 solution

Based on the error message you provided, it appears that the issue may not necessarily be related to the <anonymousidentification> tag in your web.config file, but rather to the Request.Anonymous field being null.

Here are a few things you can try to troubleshoot the issue:

Ensure that the <anonymousidentification> tag is present in the correct section of your web.config file. It should be located within the <system.web> section, like so:
XML
<configuration>
  <system.web>
    <anonymousIdentification enabled="true" cookieless="UseCookies" cookieName=".ASPXANONYMOUS" cookieTimeout="4320" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" />
    <!-- other system.web settings -->
  </system.web>
  <!-- other configuration settings -->
</configuration>


When you've validated it is placed correctly on your production environment, make sure that anonymous authentication is enabled in IIS on your production server.

To do this, open IIS Manager, select your website, and click on the "Authentication" feature. Make sure that "Anonymous Authentication" is enabled.

If you've confirmed that, you can check the version of ASP.NET running on your production server. Make sure that it is compatible with your MVC 5 project.

Check if your code is trying to access the Request.Anonymous field before the request has been authenticated. If this is the case, you need to make sure that the request is authenticated before trying to access the Request.Anonymous field. You can use the following code to check if the request is authenticated before accessing the Request.Anonymous field:
C#
if (HttpContext.Current.Request.IsAuthenticated)
{
    // Access Request.Anonymous field
}
else
{
    // Redirect to login page or show an error message
}



Lastly, you could try adding some logging or debugging statements to your code to identify the root cause of the issue. Like:

C#
Debug.WriteLine("Request.IsAuthenticated: " + HttpContext.Current.Request.IsAuthenticated);
Debug.WriteLine("Request.Anonymous: " + HttpContext.Current.Request.Anonymous);


Hope this helps you to debug your situation.
 
Share this answer
 
Comments
Graeme_Grant 3-Mar-23 9:10am    
6+-year-old question ... not sure that he is still looking for a solution.
eekayonline 3-Mar-23 10:30am    
haha crepes.. I've misread that date by 5 year.
Graeme_Grant 3-Mar-23 15:38pm    
Easily done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900