public HttpResponseMessage Post([FromBody] UserSignInViewModel model)
        {
            TdIdentityContext context = new TdIdentityContext();
            var          userStore    = new UserStore <TicketDeskUser>(context);
            var          roleStore    = new RoleStore <TicketDeskRole>(context);
            var          userManager  = new TicketDeskUserManager(userStore);
            var          roleManager  = new TicketDeskRoleManager(roleStore);
            IOwinContext context1     = HttpContext.Current.GetOwinContext();

            //UserSignInViewModel model = new UserSignInViewModel();
            //model.UserNameOrEmail = "*****@*****.**";
            //model.Password = "******";
            //model.RememberMe = false;
            HttpResponseMessage     result;
            TicketDeskSignInManager signinmanager = new TicketDeskSignInManager(userManager, context1.Authentication);
            TdDomainContext         domain        = new TdDomainContext(null);
            UserController          controller    = new UserController(userManager, signinmanager, domain);

            controller.SignInApi(model, "");

            //var response = Request.CreateResponse(HttpStatusCode.Moved);
            //response.Headers.Location = new Uri("https://localhost:44373/ticket/new") ;
            //return response;
            result = Request.CreateResponse(HttpStatusCode.OK, "https://localhost:44373/ticket/new");
            return(result);
            //var employee = "Megha";
            //if (employee == null)
            //{
            //	return NotFound();
            //}
            //return Ok(employee);
        }
 public IOrderedEnumerable <TicketDeskUser> GetUsersInRole(string roleName, TicketDeskUserManager userManager)
 {
     return(this
            .FindByName(roleName)
            .Users
            .GetUsersInRole(userManager)
            .OrderBy(u => u.DisplayName));
 }
Пример #3
0
        /// <summary>
        /// Migrates the users and roles from a legacy database to the new TD 2.5 schema.
        /// </summary>
        /// <param name="context">The identity database context</param>
        /// <param name="userManager">The user manager.</param>
        /// <param name="roleManager">The role manager.</param>
        /// <returns><c>true</c> if users migrated, <c>false</c> otherwise.</returns>
        public static bool MigrateSecurity(TicketDeskIdentityContext context, TicketDeskUserManager userManager, TicketDeskRoleManager roleManager)
        {
            EnsureRolesExist(roleManager);
            var appId =
                context.Database.SqlQuery <Guid>(
                    "select ApplicationId from aspnet_Applications where ApplicationName = 'TicketDesk'").First().ToString();
            var users = context.Database.SqlQuery <LegacyUser>(
                "select UserId, Email, Password, PasswordFormat, Comment from aspnet_Membership where ApplicationId = '" + appId + "' and IsApproved = 1 and IsLockedOut = 0").ToList();
            const string roleQuery = "SELECT r.RoleName FROM aspnet_UsersInRoles u inner join aspnet_Roles r on u.RoleId = r.RoleId WHERE u.UserId = @userId and r.ApplicationId = @appId";

            foreach (var user in users)
            {
                var newUser = new TicketDeskUser
                {
                    UserName    = user.Email,
                    Email       = user.Email,
                    DisplayName = user.Comment,
                };

                var result = user.PasswordFormat == 0 ?
                             userManager.Create(newUser, user.Password) :
                             userManager.Create(newUser);

                if (result.Succeeded)
                {
                    var rolesForUser =
                        context.Database.SqlQuery <string>(roleQuery,
                                                           new SqlParameter("userId", user.UserId),
                                                           new SqlParameter("appId", appId));
                    var newRoles = new List <string>();
                    foreach (var role in rolesForUser)
                    {
                        switch (role.ToLowerInvariant())
                        {
                        case "administrators":
                            newRoles.Add("TdAdministrators");
                            break;

                        case "helpdesk":
                            newRoles.Add("TdHelpDeskUsers");
                            break;

                        case "ticketsubmitters":
                            newRoles.Add("TdInternalUsers");
                            break;

                        default:
                            newRoles.Add("TdPendingUsers");
                            break;
                        }
                    }
                    userManager.AddToRoles(newUser.Id, newRoles.ToArray());
                }
            }
            return(true);
        }
        private void InitializeUserManager(TicketDeskUserManager manager, IAppBuilder app)
        {
            manager.UserValidator = new UserValidator <TicketDeskUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail             = true
            };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength          = 5,
                RequireNonLetterOrDigit = false,
                RequireDigit            = false,
                RequireLowercase        = false,
                RequireUppercase        = false,
            };

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault          = true;
            manager.DefaultAccountLockoutTimeSpan        = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider <TicketDeskUser>
            {
                MessageFormat = "Your security code is {0}"
            });
            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider <TicketDeskUser>
            {
                Subject    = "Security Code",
                BodyFormat = "Your security code is {0}"
            });
            manager.EmailService = new EmailService();
            manager.SmsService   = new SmsService();

            //TODO: research DpapiDataProtectionProvider and figure out what the f*** this is supposed to do
            var dataProtectionProvider = app.GetDataProtectionProvider();

            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider =
                    new DataProtectorTokenProvider <TicketDeskUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
        }
 public TicketDeskContextSecurityProvider()
 {
     UserManager = DependencyResolver.Current.GetService <TicketDeskUserManager>();
 }
 public IOrderedEnumerable <TicketDeskUser> GetTdPendingUsers(TicketDeskUserManager userManager)
 {
     return(GetUsersInRole("TdPendingUsers", userManager));
 }
 public IOrderedEnumerable <TicketDeskUser> GetTdTdAdministrators(TicketDeskUserManager userManager)
 {
     return(GetUsersInRole("", userManager));
 }
 public TicketDeskSignInManager(TicketDeskUserManager userManager, IAuthenticationManager authenticationManager)
     : base(userManager, authenticationManager)
 {
 }
 public TicketDeskContextSecurityProvider()
 {
     UserManager   = DependencyResolver.Current.GetService <TicketDeskUserManager>();
     CurrentUserId = HttpContext.Current.User.Identity.GetUserId();
 }
 public UserDisplayInfoCache(TicketDeskUserManager manager)
 {
     Manager = manager;
 }