Пример #1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                if (user.UserName == "*****@*****.**")
                {
                    var role = new MongoIdentityRole("Administrator");
                    user.AddRole(role.Id);
                }
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> Register(RegisterCreateModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                var user = new Personnel
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    PhoneNumber = "95236"
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var role = new MongoIdentityRole {
                        Name           = "normal",
                        NormalizedName = "NORMAL"
                    };
                    var resultRole = await _roleManager.CreateAsync(role);

                    await _userManager.AddToRoleAsync(user, "normal");

                    await _signInManager.SignInAsync(user, false);

                    return(RedirectToLocal(returnUrl));
                }
            }
            return(View(model));
        }
Пример #3
0
        public async Task MongoIdentityRole_CanAddAndRetrieveAndRemoveClaims()
        {
            using (var store = new MongoRoleClaimStore <MongoIdentityRole>(options, null, null, null))
            {
                var mongoRole = new MongoIdentityRole("godRole");
                var createOne = await store.CreateAsync(mongoRole, CancellationToken.None);

                var retrieveOne = await store.FindByNameAsync(mongoRole.Name, CancellationToken.None);

                Assert.NotNull(retrieveOne);
                var claim = new Claim(ClaimTypes.Role, "god");
                await store.AddClaimAsync(retrieveOne, claim);

                var retrieveOneAgain = await store.FindByNameAsync(mongoRole.Name, CancellationToken.None);

                Assert.Single(retrieveOneAgain.Claims);

                retrieveOneAgain = await store.FindByIdAsync(retrieveOneAgain.Id, CancellationToken.None);

                await store.RemoveClaimAsync(retrieveOneAgain, retrieveOneAgain.Claims.Single().ToClaim());

                retrieveOneAgain = await store.FindByIdAsync(retrieveOneAgain.Id, CancellationToken.None);

                Assert.Empty(retrieveOneAgain.Claims);
            }
        }
Пример #4
0
        public async Task <IActionResult> RegisterTrader(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                MongoDbRepository <UserToken> ut = new MongoDbRepository <UserToken>();

                if (ut.SearchFor(x => x.Token == model.Token && x.Created == false).Any())
                {
                    var userToken = ut.SearchFor(x => x.Token == model.Token).FirstOrDefault <UserToken>();

                    var role = new MongoIdentityRole();
                    role.Name = ((UserType)userToken.UserType).ToString();

                    await _roleManager.CreateAsync(role);

                    var user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email, EntityName = model.EntityName, Address1 = model.Address1, Address2 = model.Address2, PINCode = model.PINCode, GstId = model.GstId
                    };

                    var result = await _userManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        await _userManager.AddToRoleAsync(user, ((UserType)userToken.UserType).ToString());

                        userToken.Created = true;
                        ut.Update(userToken);
                        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                        // Send an email with this link
                        //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                        //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                        //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                        //    "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation(3, "User created a new account with password.");
                        return(RedirectToLocal(returnUrl));
                    }
                    AddErrors(result);
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid TOken");
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #5
0
        internal static IEnumerable <MongoIdentityRole> GetRoles()
        {
            var userRole = new MongoIdentityRole("user");

            userRole.Claims.Add(new Identity.MongoDb.Models.MongoUserClaim(new System.Security.Claims.Claim(ClaimTypes.GroupSid, Guid.NewGuid().ToString())));
            userRole.Claims.Add(new Identity.MongoDb.Models.MongoUserClaim(new Claim(ClaimTypes.PrimarySid, Guid.NewGuid().ToString())));
            var roles = new List <MongoIdentityRole>()
            {
                userRole,
                new MongoIdentityRole("administrator"),
                new MongoIdentityRole("customer")
            };

            return(roles);
        }
Пример #6
0
        public async Task MongoIdentityRole_CanBeSavedAndRetrievedAndDeleted()
        {
            using (var store = new MongoRoleClaimStore <MongoIdentityRole>(options, null, null, null))
            {
                var mongoRole = new MongoIdentityRole("godRole");
                var createOne = await store.CreateAsync(mongoRole, CancellationToken.None);

                var retrieveOne = await store.FindByNameAsync(mongoRole.Name, CancellationToken.None);

                Assert.NotNull(retrieveOne);
                await store.DeleteAsync(retrieveOne);

                retrieveOne = await store.FindByNameAsync(mongoRole.Name, CancellationToken.None);

                Assert.Null(retrieveOne);
            }
        }
Пример #7
0
        public async Task <IActionResult> RegisterSponsor(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, Folders = _actorCreator.CreateFolders()
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                var role = new MongoIdentityRole
                {
                    Name = "SPONSOR"
                };
                await _roleManager.CreateAsync(role);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");
                    await _userManager.AddToRoleAsync(user, "SPONSOR");

                    //await _folderCreator.CreateFolderAsync(user);
                    await _actorCreator.CreateSponsorAsync(user);


                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id.ToString(), code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation("User created a new account with password.");
                    return(RedirectToAction("Index", "Enterprise"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }