Skip to content

Instantly share code, notes, and snippets.

@RhysC
Created July 9, 2013 04:17
Show Gist options
  • Save RhysC/5954669 to your computer and use it in GitHub Desktop.
Save RhysC/5954669 to your computer and use it in GitHub Desktop.
Handling Anon users in ASP.Net. You can use the Anon module (include it in your web config and the anon users will get a unique token (cookie) that can be used to track them. On authentication we can handle the event of changing from anon => auth and clear the token http://brockallen.com/2012/04/07/think-twice-about-using-session-state/ http://m…
using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
ViewBag.Message = "User = " + User.Identity.Name;
else
ViewBag.Message = "AnonymousID = " + Request.AnonymousID;
return View();
}
//...
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
{
//clear the cookie, Note the Request.AnonymousID will still be present for this request
AnonymousIdentificationModule.ClearAnonymousIdentifier();
}
}
<!-- snip -->
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<!-- Track unauthorized users -->
<anonymousIdentification enabled="true" />
<!-- snip -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment