Exemplo n.º 1
0
        public async Task <IActionResult> OnGetAsync(string msgJson)
        {
            MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Index.OnGetAsync()", Page());

            try
            {
                using (IAdminRepository repository = new AdminRepository(_conn))
                {
                    var resCnt = await repository.GetRoleCountAsync();

                    rc += resCnt;
                    if (rc.IsSuccess())
                    {
                        URDCount = String.Format("URD Count = {0}", resCnt.GetResult());
                        SetPageStatusMsg("Database access ok", ExistingMsg.Keep);
                        rc.SetResult(Page());
                    }
                }
            }
            catch (Exception e)
            {
                rc.SetError(3130101, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
            }
            if (rc.IsError())
            {
                _logger.LogError(rc.GetErrorTechMsg());
                SetPageStatusMsg(rc.GetErrorUserMsgHtml(Startup.WebAppName, WebErrorHandling.GetMxRcReportToEmailBody()), ExistingMsg.Overwrite);
            }
            return(rc.GetResult());
        }
Exemplo n.º 2
0
        public async static Task <MxReturnCode <bool> > CreateRole(IAdminRepository repo, RoleManager <IdentityRole> roleManager, Guid wst, string gdprRoleName)
        {
            MxReturnCode <bool> rc = new MxReturnCode <bool>("IdentityDb.CreateRole()");

            var identityRoleName = AdminRepository.GetIdentityRolename(gdprRoleName);

            if ((repo == null) || (roleManager == null) || (gdprRoleName == null) || (identityRoleName == null))
            {
                rc.SetError(3160301, MxError.Source.Param, "repo, roleManager or gdprRoleName is null", MxMsgs.MxErrUnexpected);
            }
            else
            {
                try
                {
                    if (await roleManager.RoleExistsAsync(identityRoleName) == false)
                    {
                        IdentityResult result = await roleManager.CreateAsync(new IdentityRole { Name = identityRoleName });

                        if (result.Succeeded == false)
                        {
                            rc.SetError(3160302, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot create role {identityRoleName}"));
                        }
                    }
                    if (rc.GetErrorCode() == MxErrorLog.UnknownError)
                    {
                        //if gdprname exists - setresult

                        //create GDPR records; WST + URD and WXR record for each role 1) Admin 2) Controller  3) Standard 4) Guest 5) System (Guest) 6) Ghost (Guest)

                        //var resCnt = await repository.CreateGDPRRole(gdprRoleName != null ? gdprRoleName : identityRoleName);
                        //rc += resCnt;
                        //if (res.IsSuccess())
                        //{
                        //   rc.SetResult(true);
                        //}
                    }
                }
                catch (Exception e)
                {
                    rc.SetError(3160303, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
                }
            }
            if (rc.IsError())
            {
                //var role = await roleManager.FindByNameAsync(identityRoleName);
                //if (role != null)
                //    await roleManager.DeleteAsync(role);
            }
            return(rc);
        }
Exemplo n.º 3
0
        public void OnGet(int?statusCode = null)
        {
            MxReturnCode <bool> rc = new MxReturnCode <bool>("Error.OnGet()");

            if (statusCode.HasValue == false)
            {
                var feature = this.HttpContext.Features?.Get <IExceptionHandlerFeature>();
                rc.SetError(3140101, MxError.Source.Exception, feature?.Error?.Message ?? "[not set]", MxMsgs.MxErrUnknownException, true); //03-12-18 change to errorcde
                SetPageStatusMsg(rc.GetErrorUserMsgHtml(Startup.WebAppName, WebErrorHandling.GetMxRcReportToEmailBody()), ExistingMsg.Overwrite);
                _logger.LogError(rc.GetErrorTechMsg());
            }
            else
            {
                if (statusCode.Value == 404) //Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound
                {
                    SetPageStatusMsg("Error: Page not found. Correct the URL in your browser's address bar and try again", ExistingMsg.Overwrite);
                }
                else
                {
                    SetPageStatusMsg($"Error: Invalid request; status={statusCode}. Correct the URL in your browser's address bar and try again", ExistingMsg.Overwrite);
                }
                rc.SetResult(true);
            }
        }
Exemplo n.º 4
0
        public async static Task <MxReturnCode <int> > CreateUser(ISysRepository repo, UserManager <IdentityUser> userManager, string gdprRoleName, string password, string email, string fullName)
        {
            MxReturnCode <int> rc = new MxReturnCode <int>("IdentityDb.CreateUser()", -1);

            if ((repo == null) || (userManager == null) || (gdprRoleName == null) || (password == null) || (email == null) || (fullName == null))
            {
                rc.SetError(3160101, MxError.Source.Param, "repo, userManager, roleName, password, email, or fullname is null", MxMsgs.MxErrUnexpected);
            }
            else
            {
                try
                {
                    if (await IsExistsUser(repo, userManager, email) == true)
                    {
                        rc.SetResult(0);
                    }
                    else
                    {
                        if (await userManager.FindByEmailAsync(email) == null)
                        {
                            IdentityUser user = new IdentityUser()
                            {
                                UserName = email,
                                Email    = email
                            };
                            await userManager.CreateAsync(user, password);

                            IdentityResult result = await userManager.AddToRoleAsync(user, gdprRoleName);

                            if (result.Succeeded == false)
                            {
                                rc.SetError(3160102, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot create role {gdprRoleName}"));
                            }
                        }
                        if (rc.GetErrorCode() == MxErrorLog.UnknownError)
                        {
                            //if (await repository.IsGdprUserExists(email) )

                            //var resCnt = await repository.CreateGDPRUser();
                            //rc += resCnt;
                            //if (res.IsSuccess())
                            //{
                            //   rc.SetResult(true);
                            //}
                        }
                        rc.SetResult(1);
                    }
                }
                catch (Exception e)
                {
                    rc.SetError(3160102, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
                }
                if (rc.IsError())
                {
                    var user = await userManager.FindByEmailAsync(email);

                    if (user != null)
                    {
                        await userManager.DeleteAsync(user);
                    }
                }
            }
            return(rc);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Account.ExternalLogin.OnPostConfirmationAsync()", RedirectToPage("./Login", new { ReturnUrl = returnUrl }));

            try
            {      // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    rc.SetError(3090201, MxError.Source.Sys, "Error loading external login information during confirmation.");
                }
                else
                {
                    if (ModelState.IsValid == false)
                    {
                        rc.SetError(3090202, MxError.Source.Data, WebErrorHandling.GetModelStateErrors(ModelState, WebErrorHandling.FormValidationErrorPreamble));
                    }
                    else
                    {
                        var providerEmail = ProviderEmail;
                        if (providerEmail != Input.Email)
                        {
                            rc.SetError(3090203, MxError.Source.Sys, $"{providerEmail} from provider != {Input.Email} from form", MxMsgs.MxErrUnexpected);
                        }
                        else
                        {
                            IdentityUser user = null;
                            if (await _userManager.FindByEmailAsync(providerEmail) == null)
                            {
                                user = new IdentityUser {
                                    UserName = providerEmail, Email = providerEmail, EmailConfirmed = true
                                };
                                var result = await _userManager.CreateAsync(user);

                                if (result.Succeeded == false)
                                {
                                    rc.SetError(3090204, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot create user account for {providerEmail}"));
                                }
                            }
                            if (rc.GetErrorCode() != 3090204)
                            {
                                if ((user = await _userManager.FindByEmailAsync(providerEmail)) == null)
                                {
                                    rc.SetError(3090205, MxError.Source.Sys, $"Unable to load user {providerEmail}", MxMsgs.MxErrUnexpected, true);
                                }
                                else
                                {
                                    var result = await _userManager.AddLoginAsync(user, info);

                                    if (result.Succeeded == false)
                                    {
                                        rc.SetError(3090206, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot add  {info.LoginProvider} login  for {providerEmail}"));
                                    }
                                    else
                                    {
                                        await _signInManager.SignInAsync(user, isPersistent : false);

                                        SetPageStatusMsg($"Welcome {info.Principal.Identity.Name} you have been authenticated by {info.LoginProvider}", ExistingMsg.Overwrite);
                                        rc.SetResult(LocalRedirect(returnUrl));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                rc.SetError(3090207, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
            }
            if (rc.IsError(true))
            {
                SetPageStatusMsg(rc.GetErrorUserMsgHtml(), ExistingMsg.Overwrite);
            }

            return(rc.GetResult());
        }
Exemplo n.º 6
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            MxReturnCode <IActionResult> rc = new MxReturnCode <IActionResult>("Account.Manage.Register.OnPostAsync()", Page());

            string userId = null;

            returnUrl = returnUrl ?? Url.Content("~/");
            if (!ModelState.IsValid)
            {
                rc.SetError(3010101, MxError.Source.User, WebErrorHandling.GetModelStateErrors(ModelState, WebErrorHandling.FormValidationErrorPreamble));
            }
            else
            {
                try
                {
                    if (await ValidateForm() == false)
                    {
                        rc.SetError(3010102, MxError.Source.User, WebErrorHandling.GetModelStateErrors(ModelState, WebErrorHandling.FormValidationErrorPreamble));
                    }
                    else
                    {
                        var user = new IdentityUser {
                            UserName = Input.Email, Email = Input.Email
                        };
                        var result = await _userManager.CreateAsync(user, Input.Password);

                        if (result.Succeeded == false)
                        {
                            rc.SetError(3010103, MxError.Source.Sys, WebErrorHandling.GetIdentityErrors(result, $"cannot register user {Input.Email}"));
                        }
                        else
                        {
                            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                            var callbackUrl = Url.Page(
                                "/Account/ConfirmEmail",
                                pageHandler: null,
                                values: new { userId = user.Id, code = code },
                                protocol: Request.Scheme);

                            await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                              $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                            userId = user?.Id;
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            rc.SetResult(LocalRedirect(returnUrl));
                        }
                    }
                }
                catch (Exception e)
                {
                    rc.SetError(3010104, MxError.Source.Exception, e.Message, MxMsgs.MxErrUnknownException, true);
                }
            }
            if (rc.IsError(true))
            {
                SetPageStatusMsg(rc.GetErrorUserMsgHtml(userId), ExistingMsg.Overwrite);
            }

            return(rc.GetResult());
        }