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!

EDIT: When I add: NavigationManager.NavigateTo("/",true);

I get this error:

at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, NavigationOptions options)       at Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(String uri, Boolean forceLoad)       at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad, Boolean replace)       at Project.Components.Account.Pages.DeletePersonalData.<DeleteAccount>d__22.MoveNext() in C:\Users\baswi\OneDrive\_DEV\PS\Github\Project\wa-Project\Components\Account\Pages\DeletePersonalData.razor.cs:line 147

I added it after the sign out method:

   await SignInManager.SignOutAsync();   NavigationManager.NavigateTo("/");

Before the sign out method, or with or without forceLoad, it doesn't change anything.

So, this does work (in static mode), but I still have no idea why this does work, but the above doesn't?

private async Task DeleteAccount(){    if (AbleToDeleteAccount == true)    {        await reports.RemoveFavoriteAsync(CancellationToken, user.Id, null);        await reports.SetVoteAsync(CancellationToken, user.Id, null, 0, 0, true);        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();                await AuditLog.AddAuditLogAsync(null, company, null, "✅", $"User removed personal account: {user.UserName}", company!.Id.ToString(), "Delete", CancellationToken);            }        }        await SignInManager.SignOutAsync();        NavigationManager.NavigateTo("/", true);    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>