Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

User deleting personal data DeleteAccount() in blazor and Microsoft Identity library

$
0
0

Alright, I’m pretty lost here, and the internet isn’t much help — there are so many posts about this, but everyone seems to have their own version of the code. I’m just a hobby programmer working with Blazor, C#, and SQL. I’m building a web app for fun, but this part is draining so much energy that I’m not sure what to do anymore.

My app normally runs in InteractiveServer mode, but for the /DeletePersonalData page it falls back to static because of this call:

user = await UserAccessor.GetRequiredUserAsync(HttpContext);

In the Oninitialized.And to make sure It's on static I use this:

@attribute [ExcludeFromInteractiveRouting]

I have verified this.

I want users to be able to delete their accounts and then navigate back to either the homepage or login page. But since this is running in static mode, I can’t use Blazor’s built-in NavigationManager and SignInManager.SignOutAsync() doesn’t help either.

This is the method I’m running:

private async Task DeleteAccount(){    if (AbleToDeleteAccount == true)    {        try        {            await reports.RemoveFavoriteAsync(CancellationToken, user.Id, null);            await reports.SetVoteAsync(CancellationToken, user.Id, null, 0, 0, true);            var logins = await UserManager.GetLoginsAsync(user);            foreach (var login in logins)            {                var result2 = await UserManager.RemoveLoginAsync(user, login.LoginProvider, login.ProviderKey);                if (!result2.Succeeded)                {                    throw new InvalidOperationException("Unexpected error occurred removing user login.");                }            }            var result = await UserManager.DeleteAsync(user);            if (!result.Succeeded)            {                throw new InvalidOperationException("Unexpected error occurred deleting user.");            }            if (user.CustomerId != null)            {                var companies = await CustomerService.GetCustomer(CancellationToken, null, user.CustomerId);                if (companies.Count > 0)                {                    var company = companies.FirstOrDefault();                    var auditLog = await AuditLog.AddAuditLogAsync(null, company, null, "✅", $"User removed personal account: {user.UserName}", company!.Id.ToString(), "Delete", CancellationToken);                }            }            await SignInManager.SignOutAsync();        }        catch (Exception ex)        {            toastService.AddToast("Error", "An error occurred while deleting your account.", "error");        }    }}

I’d prefer to run this in InteractiveServer mode, but everything I’ve found online tells me it’s not possible. I also can’t get the redirect to another page working. I understood that SignOutAsync() should handle the redirect already, but nothing happens. I’ve tried too many things at this point, and now the whole thing has become one big clobber of a mess.

Can anyone help me to the right documentation, or at least give me a hint? Thanks in advance!


Viewing all articles
Browse latest Browse all 4839

Trending Articles