Created
July 9, 2013 04:17
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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