SocialPackage.WebApiApplication.Application_PostAuthenticateRequest C# (CSharp) Method

Application_PostAuthenticateRequest() protected method

protected Application_PostAuthenticateRequest ( Object sender, EventArgs e ) : void
sender Object
e System.EventArgs
return void
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            var cu = Context.User;
            EfDbContext db = new EfDbContext();
            if (db.Users.Count() != 0 && db.Users.Any(c => c.Roles.Any(r => r.RoleName == "Администратор")))
            {

                string currentSid = null;
                var identity = Context.User.Identity as WindowsIdentity;
                if (identity.User != null) currentSid = identity.User.Value;

                SocialPackage.Infrastructure.Entities.User usr = db.Users.Include("Limit").FirstOrDefault(el => el.Login == currentSid);
                if (usr != null)
                {
                    usr.Identity = identity;
                    Context.User = usr;
                    //throw new MembershipCreateUserException("заведите пользователя в базе данных");
                }

            }
            else
            {
                var user = new User
                {
                    Roles = new List<UserRole>
                    {
                        new UserRole
                        { RoleName = "Администратор" }
                    }
                };

                user.Identity = Context.User.Identity as WindowsIdentity;
                Context.User = user;
            }
        }