Пример #1
0
        //Create Userrole must be deleted after initialisation, I've let it stand because it's a testCase with a local db
        private async Task CreateUserRoles(IServiceProvider serviceProvider)
        {
            RoleManager <IdentityRole> roleManager    = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();
            FoodiesContext             foodiesContext = serviceProvider.GetRequiredService <FoodiesContext>();

            IdentityResult roleResult;
            //Adding Admin Role
            bool roleCheck = await roleManager.RoleExistsAsync("Admin");

            if (!roleCheck)
            {
                // create roles and seed them to the datebase
                roleResult = await roleManager.CreateAsync(new IdentityRole("Admin"));
            }
            bool loggedInUserRoleCheck = await roleManager.RoleExistsAsync("LoggedInUser");

            if (!loggedInUserRoleCheck)
            {
                roleResult = await roleManager.CreateAsync(new IdentityRole("LoggedInUser"));
            }
            // Assign Admin Role to the main user
            ApplicationUser user = new ApplicationUser();

            user.UserName = "******";
            user.Email    = "*****@*****.**";
            PasswordHasher <ApplicationUser> password = new PasswordHasher <ApplicationUser>();

            user.PasswordHash       = password.HashPassword(user, "Test1234!");
            user.EmailConfirmed     = true;
            user.NormalizedEmail    = user.Email.ToUpper();
            user.NormalizedUserName = user.UserName.ToUpper();
            user.EndUser            = new EndUser
            {
                NickName = "LazyJay",
                Name     = "Lazy",
                LastName = "Jay"
            };
            foodiesContext.Users.Add(user);
            if (user != null)
            {
                DbSet <IdentityUserRole <string> > roles = foodiesContext.UserRoles;
                IdentityRole adminRole = foodiesContext.Roles.FirstOrDefault(roles => roles.Name == "Admin");
                if (adminRole != null)
                {
                    if (!roles.Any(Uri => Uri.UserId == user.Id && Uri.RoleId == adminRole.Id))
                    {
                        roles.Add(new IdentityUserRole <string> {
                            UserId = user.Id, RoleId = adminRole.Id
                        });
                        foodiesContext.SaveChanges();
                    }
                }
            }
        }
Пример #2
0
 public RegisterModel(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     ILogger <RegisterModel> logger,
     IEmailSender emailSender,
     FoodiesContext context)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _logger        = logger;
     _emailSender   = emailSender;
     _context       = context;
 }
Пример #3
0
 public MealIngredientController(FoodiesContext context)
 {
     _context = context;
 }
Пример #4
0
 public UnitController(FoodiesContext context)
 {
     _context = context;
 }
Пример #5
0
 public HomeController(FoodiesContext context)
 {
     _context = context;
 }
Пример #6
0
 public CategoryController(FoodiesContext context)
 {
     _context = context;
 }
Пример #7
0
 public UnitOfWork(FoodiesContext context)
 {
     _context = context;
 }
Пример #8
0
 public EndUserController(FoodiesContext context)
 {
     _context = context;
 }
Пример #9
0
 public LevelPreperationController(FoodiesContext context)
 {
     _context = context;
 }
Пример #10
0
 public PreperationTimeController(FoodiesContext context)
 {
     _context = context;
 }
Пример #11
0
 //https://www.c-sharpcorner.com/article/upload-and-display-image-in-asp-net-core-3-1/
 public MealController(FoodiesContext context, UserManager <ApplicationUser> userManager, IWebHostEnvironment hostEnvironment)
 {
     _context           = context;
     _userManager       = userManager;
     webHostEnvironment = hostEnvironment;
 }