コード例 #1
0
        public async Task <IActionResult> Demo()
        {
            var translate = new Translate();

            var demoData   = new DemoData(dbContext, translate);
            var uniqDemoId = await demoData.CreateDemo(Request.HttpContext.Connection.RemoteIpAddress.ToString());

            var user1FullName = translate.Get("DEMO.YOUR_FULL_NAME");
            var user1Username = $"{uniqDemoId}[email protected]";
            var user1         = new ApplicationUser {
                FullName = user1FullName, UserName = user1Username, Email = user1Username
            };

            ThrowErrors(await userManager.CreateAsync(user1, Guid.NewGuid().ToString() + "Ab@1"));
            var partitionId = CreateNewPartitionAndOrganizationForUser(user1, Plans.Demo, translate.Get("DEMO.ORGANIZATION_NAME"), translate.Get("DEMO.ORGANIZATION_ADDRESS"));

            ThrowErrors(await userManager.AddClaimAsync(user1, new Claim(CustomClaimTypes.Partition, Convert.ToString(partitionId))));
            ThrowErrors(await userManager.AddClaimAsync(user1, new Claim(CustomClaimTypes.Demo, "true")));

            var user2FullName = translate.Get("DEMO.ANOTHER_USERS_FULL_NAME");
            var user2Username = $"{uniqDemoId}[email protected]";
            var user2         = new ApplicationUser {
                FullName = user2FullName, UserName = user2Username, Email = user2Username
            };

            ThrowErrors(await userManager.CreateAsync(user2, Guid.NewGuid().ToString() + "Ab@1"));
            ThrowErrors(await userManager.AddClaimAsync(user2, new Claim(CustomClaimTypes.Partition, Convert.ToString(partitionId))));
            ThrowErrors(await userManager.AddClaimAsync(user2, new Claim(CustomClaimTypes.Demo, "true")));

            telemetryClient.TrackEvent("DemoUsersSuccess", new Dictionary <string, string> {
                { "partitionId", Convert.ToString(partitionId) }, { "user1Username", user1Username }, { "user2Username", user2Username }
            });

            demoData.AddDemoData(partitionId, user1, user2);
            await dbContext.SaveChangesAsync();

            telemetryClient.TrackEvent("DemoSuccess", new Dictionary <string, string> {
                { "partitionId", Convert.ToString(partitionId) }
            });

            await signInManager.SignInAsync(user1, isPersistent : false);

            return(RedirectToAction("Index", "A"));
        }