Accesing Identity Profile Management - asp.net-mvc

I'm stuck with this problem for over 2 hours and can't find the answer anywhere.
I have a project that uses Identity. I have created a register and login not using Identity scaffolded items from the beginning.
Next, I added custom fields to user identity using ApplicationUser:IdentityUSer.
Now I want to allow users to manage their profiles. I wanted to use the default Identity Manager and then customize it. I didn't have it though so I added a new scaffolded Identity with only "Identity/Pages/Account/Manage" item. It has created an area.
The problem is that I don't know how to access the Index page inside this area in my _LoginPartial view.
Part of _LoginPartial code:
#if (SignInManager.IsSignedIn(User))
{
<ul class="navbar-nav flex-grow-1 justify-content-end text-center">
<li class="nav-item mr-5 text-center">
<a //here i want to access Index page class="nav-link btn">Hello, #User.Identity.Name</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Account" asp-action="Logout">Logout</a>
</li>
</ul>
}
else
{
}
My project structure:
Is this a good way to manage user profile though?

To have your first <a> tag link to your new Account | Manage page, you can include the asp-area, asp-controller and asp-action attributes. (similar to what you already have on your second <a> tag).
Try changing the line to this:
<a asp-area="Identity" asp-controller="Account" asp-action="Manage" class="nav-link btn">Hello, #User.Identity.Name</a>

Related

How to structure a .NET Core MVC project for multiple user roles?

Assuming you had multiple user roles (such as Customer, Employee, Manager) and wanted to provide a personalised experience (different home page, different pages accessable by different roles), what would be the best way to set this up to make it scalable?
Currently we are considering using areas for the different roles.
E.G. A manager would be directed to the manager area home page when logging in whereas a customer would be directed to the customer area home page.
Is there a better way to structure this?
Normally we redirect user to different pages via navigation menu/bar in web site, if you'd like to show different pages based on user's role, you can dynamically render/display navigation menu/bar by checking current user's role after user signed into site.
#if (!SignInManager.IsSignedIn(User))
{
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">WebApp</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<partial name="_LoginPartial" />
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
}
else
{
if (User.IsInRole("manager"))
{
//navigation bar for manager
//html content here
//...
<a class="nav-link text-dark" asp-area="Manager" asp-controller="controller_name_here" asp-action="action_name_here">Admin Home</a>
}
else if (User.IsInRole("employee"))
{
//navigation bar for employee
}
else
{
//navigation bar for employee
}
Currently we are considering using areas for the different roles.
Areas provide a way to partition an ASP.NET Core Web app into smaller functional groups. Under the scenario you mentioned, grouping functionality within Customer, Employee and Manager units, which would make project structure more clear.

How to Redirect to controller Form Shared Layout View using Nav menu

I am working in Asp.net MVC 5. In shared layout,i am using nav to redirect to Index View of Controller but its not working. showing error Resources cannot found
Following is the code of NAV
<ul class="sidebar navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="~/Controllers/ContractorsController.cs/Index.cshtml">
<i class="fas fa-fw fa-chart-area"></i>
<span>Contractors</span>
In these Controllers i want to redirect to ContractorsControllers
And to Index View of Contractors Controller.
In MVC you cannot directly navigate to View. Use below code.
<a class="nav-link" href="#Url.Action("Index", "Contractors")">

data-toggle="tab" and Anchor Tag Helper

I can't get the anchor Tag helper to work in combination with data-toggle = tag.
I manage to set the active class on the correct tab but my code on my controller will never be run. What am I missing?
<div class="navbar">
<div>
<ul class="nav nav-tabs">
<li class="active"><a asp-controller="Customer" asp-action="Device" asp-fragment="PlantInfo" data-toggle="tab" >Enhetsinformation</a></li>
<li><a asp-controller="Customer" asp-action="Start" data-toggle="tab" >Larm</a></li>
<li><a asp-controller="Customer" asp-action="Start" data-toggle="tab" >Historik</a></li>
<li><a asp-controller="Customer" asp-action="MeterReading" asp-fragment="MeterReading" data-toggle="tab" >Avläsningar</a></li>
<li><a asp-controller="Customer" asp-action="Start" data-toggle="tab" >Ă„renden</a></li>
</ul>
</div>
</div>
Tabs are not really supposed to actually hit your controller. The whole point is that you're switching between content on page. The Bootstrap tab javascript is going to prevent the default action of the link such that it doesn't actually take you to a new page, but just loads the reference content area. That's also why the actual href of your tab links should be targets, point to the ids of the various content areas on your page:
Content 1
...
<div id="content1">
Tab content
</div>
If I had to guess at your confusion here, I'd imagine you're thinking that by linking to an actual controller action, that content should be fetched from the server and rendered on the page when you click that tab. That's incorrect and not how Bootstrap tabs work. If you wanted it to function that way, you'd need your own JavaScript to define the tab switching behavior (Bootstrap's code will not work anymore for this) and as part of that, you'd need to issue AJAX requests to fetch that content. That's all on you. It doesn't happen automatically.

How to give external link to bootstrap tabs

I am using bootstrap tabs in my ASP.NET MVC project. In this tab, I need to give external link so when users click on particular tab it will redirect to particular link.
Basically I have four controller. Now I need to redirect to each controller when user clicks on tab.
Below is the code I tried to use but it is not working :
<ul class="nav nav-tabs" role="tablist">
<li class="active">Home</li>
<li>Profile</li>
<li>Messages</li>
<li>Settings</li>
</ul>
Update :
If we remove data-toggle="tab", then it will loose tab functionality. I mean when i click on tab, it loads page and redirect. So my question is, can we redirect to other controller without loading page like what we are doing now and make it works like tab?
Just remove the data-toggle="tab" attribute and it will work as you expected.
Demo
<ul class="nav nav-tabs" role="tablist">
<li class="active">Home
</li>
<li>Profile
</li>
<li>Messages
</li>
<li>Settings
</li>
</ul>

Insert ActionLink into existing tag

I have a default internet application template from MVC4 and I would like to add some links to the <nav> tag depending on the user logged in.
I.e all the users have a default navigation panel, but if it is an admin logged in, he should have extra link for Managing product stock.
Right now it looks like that(from _Layout.cshtml):
<nav>
<ul id="menu">
<li>#Html.ActionLink("Home", "Browse", "Product")</li>
...
</ul>
</nav>
And I need somehow insert new ActionLink. I've tried to make an if statement inside the default layout, but it does not work. How could I add an extra <li> tag inside this <nav>? I am not familiar with JavaScript or JQuery, but can it be done using MVC features?
Yes this can be achieved using the razor syntax
<nav>
<ul id="menu">
<li>#Html.ActionLink("Home", "Browse", "Product")</li>
#if( User.IsInRole("Admin") ){
<li>#Html.ActionLink("Admin", "...", "...")</li>
}
</ul>
</nav>

Resources