0

I have a partial named _Username which displays the currently logged in users menu and allows the user to change settings etc...

I need to check whether the current user has a confirmed email address in the database from this partial but for some reason can't get any information from the database in relation to the confirmed email column. I can get the username as you can see from the code below.

This is my partial code:

<div class="username">
    <ul class="username__user">
        <li>
            <a asp-page="/Account/Manage/Index">@UserManager.GetUserName(User)</a>

            <ul class="username__menu">
                <li>Account</li>
                <li>Settings</li>
                <li>Messages</li>
                <li>
                    <hr />
                    <form id="logoutForm" asp-page="/Account/Logout">
                        <button id="logout" class="btn btn--green" type="submit">Logout</button>
                    </form>
                </li>
            </ul>

        </li>
    </ul>
</div>

This partial is embedded inside the _Layout partial as well. Any information regarding getting access to the UserManager would be appreciated. I do have the @inject UserManager UserManager declaration inside my _ViewImports as well.

mason
  • 31,774
  • 10
  • 77
  • 121
  • "I need to check whether the current user has a confirmed email address in the database from this partial" - nope, that's backwards. Views should not reach out to databases. Controllers are responsible for orchestrating things. Controller should reach out to data layer, get the data needed by the View(s) and pass it to them via the Model. – mason Oct 02 '22 at 20:21

1 Answers1

0

So after some digging around I did come across a solution. By adding the following at the beginning of the partial I was able to set a bool variable to pull whether the current logged in user has a confirmed email or not.

AppUser usr = await UserManager.GetUserAsync(User);
bool confirmed = await UserManager.IsEmailConfirmedAsync(usr);

This allows me to check what I needed to check so I can notify the user in my partial view when he does not have a confirmed email address.