Example #1
0
        private void ConfigureAuth(IAppBuilder app, IContainer container)
        {
            AntiForgeryConfig.UniqueClaimTypeIdentifier = DAG.Security.Safe.Owin.ClaimTypes.NameIdentifier;

            const string OwinKeyForUserManager = "AUTH::UserManager";


            /* Used to store in Owin context the resolved 'AppUserManager' and use it in the safe authentication middleware */
            app.Use(async (context, next) =>
            {
                context.Set(OwinKeyForUserManager, container.Resolve<AppUserManager>());
                await next.Invoke();
            });



#if DEBUG || DEV
            app.UseSafeDebug(new[]
            {
                new DebugUser
                {
                    FirstName = "Florent",
                    LastName = "ATLAS",
                    Id = "florent.atlas",
                    Email = "*****@*****.**",
                    ServiceName = "RESG/BSC/SMG",
                    FullName = "Florent ATLAS",
                },
                new DebugUser
                {
                    FirstName = "Ingrid",
                    LastName = "BROUSSILLON",
                    Id = "A228967",
                    Email = "*****@*****.**",
                    ServiceName = "RESG/BSC/SMG",
                    FullName = "Ingrid BROUSSILLON",
                },
                new DebugUser
                {
                    FirstName = "Laurent",
                    LastName = "BISCONDI",
                    Id = "X133876",
                    Email = "*****@*****.**",
                    ServiceName = "RESG/BSC/SMG",
                    FullName = "Laurent BISCONDI",
                }
            });
#endif

            app.UseSafe(new SafeAuthenticationOptions
            {
                CookieName = string.Format(".SAFE::{0}::{1}", ConfigurationManager.AppSettings["AppName"],ConfigurationManager.AppSettings["Environment"]),
                OnValidateIdentity = async context =>
                {
                    var userManager = context.OwinContext.Get<AppUserManager>(OwinKeyForUserManager);

                    var rtfeId = context.Identity.GetUserName();

                    var user = await userManager.FindByNameAsync(rtfeId);
                    if (user != null)
                    {
                        IList<string> roles = await userManager.GetRolesAsync(user.Id);
                        if (roles != null)
                        {
                            context.AddRoles(roles);
                            return;
                        }
                    }

                    //Add default role here if needed.
                    context.Identity.AddClaim(new Claim(System.Security.Claims.ClaimTypes.Role, "Anonymous"));
                }
            });
        }