public async Task <IActionResult> CreateAuthenticationTicketByPassword()
        {
            var request = HttpContext.GetOpenIdConnectRequest();

            if (request.IsPasswordGrantType())
            {
                var applicationUser = userManager.FindByNameAsync(request.Username).Result;
                if (applicationUser != null)
                {
                    if (!userManager.CheckPasswordAsync(applicationUser, request.Password).Result)
                    {
                        if (userManager.SupportsUserLockout)
                        {
                            userManager.AccessFailedAsync(applicationUser).Wait();
                        }

                        return(Unauthorized());
                    }
                    else
                    {
                        AuthenticationTicket ticket = CreateTicketAsync(applicationUser, request.GetResources(), request.GetScopes()).Result;
                        var result = SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
                        return(result);
                    }
                }
                else
                {
                    return(Unauthorized());
                }
            }
            else if (request.IsRefreshTokenGrantType())
            {
                AuthenticateResult info = await HttpContext.AuthenticateAsync(OpenIdConnectServerDefaults.AuthenticationScheme);

                ApplicationUser applicationUser = await userManager.GetUserAsync(info.Principal);

                if (!await signInManager.CanSignInAsync(applicationUser))
                {
                    return(Unauthorized());
                }

                AuthenticationTicket ticket = await CreateTicketAsync(applicationUser, request.GetResources(), request.GetScopes(), info.Properties);

                return(SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme));
            }

            return(Json(new OpenIdConnectResponse
            {
                Error = OpenIdConnectConstants.Errors.UnsupportedGrantType
            }));
        }
Пример #2
0
        public static void CreateTenantUsers(IUnitOfWork unitOfWork, IIdentityUserManager userManager)
        {
            Dictionary <Tenant, ApplicationUser> userTenants = new Dictionary <Tenant, ApplicationUser>();

            foreach (var tenant in unitOfWork.TenantsDBSet.Where(x => x.Name != "EmptyTenant").Include(x => x.Users).ToList())
            {
                var applicationUser = CreateApplicationUser($"{tenant.Name}_FirstName", $"{tenant.Name}_LastName", $"{tenant.Name}[email protected]", "PlainCore100", true, userManager);
                userTenants.Add(tenant, applicationUser);
            }

            unitOfWork.SaveChanges();

            foreach (var item in userTenants)
            {
                CreateUser(unitOfWork, userManager.FindByNameAsync(item.Value.Email).Result, item.Key);
            }

            unitOfWork.SaveChanges();
        }