public async Task <IActionResult> Create([Bind("Id,ConcurrencyStamp,CreatedDate,Description,Ipaddress,Name,NormalizedName")] AspNetRole aspNetRole)
        {
            if (ModelState.IsValid)
            {
                _context.Add(aspNetRole);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(aspNetRole));
        }
        public async Task <IActionResult> Create([Bind("UserId,RoleId")] AspNetUserRole aspNetUserRole)
        {
            if (ModelState.IsValid)
            {
                _context.Add(aspNetUserRole);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", "aspNetUsers", new { id = aspNetUserRole.UserId }));
            }
            ViewData["RoleId"] = new SelectList(_context.AspNetRoles.OrderBy(r => r.Name), "Id", "Name");
            return(View(aspNetUserRole));
        }
Exemple #3
0
        public async Task <IdentityResult> CreateAsync(Role role, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (role == null)
            {
                throw new ArgumentNullException(nameof(role));
            }
            _context.Add(role);
            role.ConcurrencyStamp = Guid.NewGuid().ToString();
            await _context.SaveChangesAsync(cancellationToken);

            return(IdentityResult.Success);
        }
Exemple #4
0
        public async Task <IdentityResult> CreateRefreshTokenAsync(RefreshToken refreshToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (refreshToken == null)
            {
                throw new ArgumentNullException(nameof(refreshToken));
            }

            _context.Add(refreshToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(IdentityResult.Success);
        }
Exemple #5
0
        public async Task <IdentityResult> CreateAsync(User user, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            _context.Add(user);
            user.ConcurrencyStamp = Guid.NewGuid().ToString();
            user.CreationDate     = DateTime.Now;
            user.Active           = true;
            await _context.SaveChangesAsync(cancellationToken);

            return(IdentityResult.Success);
        }
Exemple #6
0
        /// <summary>
        /// Updates the database.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="env">The environmental variable.</param>
        private static void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment env)
        {
            using IServiceScope serviceScope = app.ApplicationServices
                                               .GetRequiredService <IServiceScopeFactory>()
                                               .CreateScope();
            using IdentityDbContext context = serviceScope.ServiceProvider.GetService <IdentityDbContext>();
            context.Database.Migrate();
            List <IdentityUser> identityUsers = TestUsers.GetDefaultIdentityUsers();

            foreach (IdentityUser identityUser in identityUsers.Where(identityUser => !context.IdentityUser.Any(e => e.SubjectId == identityUser.SubjectId)))
            {
                if (env.IsProduction())
                {
                    identityUser.Password = TestUsers.CreateTestUserPassword(identityUser.Username);
                }
                context.Add(identityUser);
            }
            context.SaveChanges();
        }
Exemple #7
0
        public IActionResult AddRoles()
        {
            if (User.Identity.Name != "*****@*****.**")
            {
                return(NotFound());
            }

            var roleId = _context.AspNetRoles.Where(r => r.Name == "Developer").FirstOrDefault()?.Id;


            if (roleId == null)
            {
                AspNetRole aspNetRole = new AspNetRole();
                aspNetRole.Id = Guid.NewGuid().ToString();
                aspNetRole.ConcurrencyStamp = Guid.NewGuid().ToString();
                //aspNetRole.CreatedDate = DateTime.Now;
                aspNetRole.Name           = "Developer";
                aspNetRole.NormalizedName = "DEVELOPER";
                //aspNetRole.Description = "Developer Application Role";
                _context.Add(aspNetRole);
                try
                {
                    _context.SaveChanges();
                }
                catch (Exception)
                {
                    throw;
                }
            }

            roleId = _context.AspNetRoles.Where(r => r.Name == "Admin").FirstOrDefault()?.Id;
            if (roleId == null)
            {
                var aspNetRole = new AspNetRole();
                aspNetRole.Id = Guid.NewGuid().ToString();
                aspNetRole.ConcurrencyStamp = Guid.NewGuid().ToString();
                //aspNetRole.CreatedDate = DateTime.Now;
                aspNetRole.Name           = "Admin";
                aspNetRole.NormalizedName = "ADMIN";
                //aspNetRole.Description = "Admin Application Role";
                _context.Add(aspNetRole);
                try
                {
                    _context.SaveChanges();
                }
                catch (Exception)
                {
                    throw;
                }
            }
            var userId = _context.AspNetUsers.Where(u => u.Email == User.Identity.Name).FirstOrDefault()?.Id;

            roleId = _context.AspNetRoles.Where(r => r.Name == "Developer").FirstOrDefault()?.Id;

            if (userId == null)
            {
                return(NotFound());
            }

            AspNetUserRole aspNetUserRole = new AspNetUserRole();

            aspNetUserRole.UserId = userId;
            aspNetUserRole.RoleId = roleId;
            _context.Add(aspNetUserRole);
            try
            {
                _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
            roleId = _context.AspNetRoles.Where(r => r.Name == "Admin").FirstOrDefault()?.Id;

            if (userId == null)
            {
                return(NotFound());
            }

            aspNetUserRole        = new AspNetUserRole();
            aspNetUserRole.UserId = userId;
            aspNetUserRole.RoleId = roleId;
            _context.Add(aspNetUserRole);
            try
            {
                _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
            return(RedirectToAction(nameof(Index)));
        }