Ejemplo n.º 1
0
        /// <summary>
        /// Logs a Sign in event.
        /// </summary>
        /// <param name="identity">The created Claims Identity during sign-ing.</param>
        /// <param name="accessIpAddress">The IP address of the originating request.</param>
        /// <returns>A void Task object</returns>
        public async Task LogLoginEventAsync(ClaimsIdentity identity, string accessIpAddress)
        {
            if (string.IsNullOrWhiteSpace(accessIpAddress))
            {
                throw new ArgumentException("You must provide a valid IP address.");
            }

            // Get/Create user
            var claim = identity.Claims.FirstOrDefault(c => c.Type == Constants.CLAIMS_OBJECTIDENTIFIER);

            // Set IP as extra data
            var signInIpAddress = UserIdentityFactory.GetClaimValue("ipaddr", identity.Claims);

            var loginEvent = new Models.Event();

            loginEvent.DateTime        = DateTime.UtcNow;
            loginEvent.ActedByUser     = claim.Value;
            loginEvent.ActedByUser     = claim.Value;
            loginEvent.Type            = Enums.EventType.Signin.ToString();
            loginEvent.Data            = "AADSignInIpAddress: " + signInIpAddress;
            loginEvent.RemoteIpAddress = accessIpAddress;

            await _dbContext.Events.AddAsync(loginEvent);

            var updatedRowCount = await _dbContext.SaveChangesAsync();

            if (updatedRowCount > 1)
            {
                // we have a problem
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates or returns the <see cref="ApplicationUser"/> representing a passed IIdentity.
        /// This method checks the database first.
        /// </summary>
        /// <param name="identity">The IIdentity to resolve and map to an <see cref="ApplicationUser"/></param>
        /// <returns>A Task with the <see cref="ApplicationUser"/> as a result</returns>
        public async Task <ApplicationUser> GetCurrentUser(IIdentity identity)
        {
            if (identity == null)
            {
                return(null);
            }
            if (identity.IsAuthenticated == false)
            {
                return(null);                                                   // not authenticated, so we shouldn't build an object
            }
            var existingUser = await FindUserAsync(identity as ClaimsIdentity); // get from database first

            ApplicationUser currentApplicationUser;

            if (existingUser != null)
            {
                currentApplicationUser = existingUser;
            }
            else
            {
                // Creates a new ApplicationUser object, watch out for duplication of entities in the DB
                currentApplicationUser = UserIdentityFactory.CreateNewApplicationUserFromAzureIdentity(identity as ClaimsIdentity);
            }

            // Set it in the HttpContext items collection for the current request
            UpdateUserInItemsCollection(currentApplicationUser);

            return(currentApplicationUser);
        }
Ejemplo n.º 3
0
        public async Task StoreLoginEventAsync(ClaimsIdentity identity)
        {
            var loginEvent = new Event();

            loginEvent.DateTime = DateTime.UtcNow;
            loginEvent.Type     = Enums.EventType.Signin;
            loginEvent.User     = UserIdentityFactory.CreateApplicationUser(identity);

            await DbContext.Events.AddAsync(loginEvent);

            await DbContext.SaveChangesAsync();
        }
Ejemplo n.º 4
0
        // TODO: Implement with Func
        public async Task <ApplicationUser> ResolveUserByUpnAsync(string upn, string currentUserId)
        {
            var graphClient = GetAuthenticatedClient(currentUserId);

            try
            {
                var user       = await graphClient.Users[upn].Request().GetAsync();
                var mappedUser = UserIdentityFactory.MapApplicationUser(user);
                mappedUser.TenantId = _tenantId;
                return(mappedUser);
            }
            catch (ServiceException graphException)
            {
                _telemetryService.TrackException(graphException);
                return(null);
            }
        }