SignInManager.IsSignedIn(User) returns false on identity scaffolding - asp.net-core-mvc

using default code for Identity in Blazor I have seen that by using authentication cookies the user isn't set as logged, while the cookie exist in the browser and the link/action to be signed out wasn't callled
What could I possibly need to check what would be causing this behavior?
the only line of code I had used is a js code to call automatically the signout action on post which only exist in the logout view(which is only called in the lougout link, but for example calling the login page shows as if it wasnt logged in(which is checked at the loging partial view
<script>
(() =>
{
document.getElementById('logoutFormLocal').submit();
})();
</script>```
#using Microsoft.AspNetCore.Identity
#inject SignInManager<IdentityUser> SignInManager
#inject UserManager<IdentityUser> UserManager
<ul class="navbar-nav">
#if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a id="manage" class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello #UserManager.GetUserName(User)!</a>
</li>
<li class="nav-item">
<form id="logoutForm" class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="#Url.Page("/Index", new { area = "" })">
<button id="logout" type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" id="register" asp-area="Identity" asp-page="/Account/Register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" id="login" asp-area="Identity" asp-page="/Account/Login">Login</a>
</li>
}
</ul>

Related

Why opening bootstrap modal using wire:click and dispatch event Blocks Scrolling in admin menu

I have a problem when I use the following code for adding and displaying:
<a href="javascript:void(0)" data-toggle="modal" data-target="#modalYear">
<span class="sub-item">Add year</span>
</a>
<a href="javascript:void(0)" data-toggle="modal" data-target="#ListYear">
<span class="sub-item">Listyear</span>
</a>
Nothing gets blocked but the problem comes when I want to use the edit option the admin menu stops scrolling, this is where the problem starts and also when I use this :
<a href="javascript:void(0)" wire:click="ShowReportModal('add')">
span class="sub-item">Add Year</span>
</a>
the modal will show up but when I close it the admin menu stops scrolling.
After so many tries I figured out that the problem is using wire:click to call the modal for adding, editing and displaying.
this is my component:
public function ShowReportModal($action)
{
if($action == 'add')
{
$this->dispatchBrowserEvent('ModalYearVisible') ;
}
else{
$this->dispatchBrowserEvent('ModalListVisible') ;
}
}
Here is the admin menu code:
<li class="nav-item">
<a data-toggle="collapse" href="#annee">
<i class="far fa-calendar-alt"></i>
<p>Scholar year</p>
<span class="caret"></span>
</a>
<div class="collapse" id="annee">
<ul class="nav nav-collapse">
<li>
<a href="javascript:void(0)" wire:click="ShowReportModal('add')">
<span class="sub-item">Add year</span>
</a>
</a>
</li>
<li>
<a href="javascript:void(0)" wire:click="ShowReportModal('list')">
<span class="sub-item">Years List</span>
</a>
</li>
</ul>
</div>
</li>
Any help would be appreciated and also why is this happening?

NavBar using the controller for the page it is on instead of the controller set in ActionLink

I am new to MVC and bootstrap and have a problem with the navbar. When I am on the home page the navbar works like it is suppose to, but when I change to a new page which uses the ActiveUser controller. The controller in the link changes from the Homecontroller to the ActiveUser controller which is not what suppose to happen. The dropdown menu part is just a test to see if it does the same and it does.
NavBar code
<nav class="navbar navbar-expand-md">
<div class="container">
#Html.ActionLink("TADS", "About", "Home", new { #class = "navbar-brand" })
<div class="navbar-nav mr-auto">
<ul class="navbar-nav nav">
<li class="nav-item">
#Html.ActionLink("Home", "Index", "Home", new { #class = "nav-link" })
</li>
<li class="nav-item">
#Html.ActionLink("About", "About", "Home", new { #class = "nav-link" })
</li>
<li class="nav-item">
#Html.ActionLink("Contact", "Contact", "Home", new { #class = "nav-link" })
</li>
<li class="dropdown">
Dropdown
<div class="dropdown-menu">
#Html.ActionLink("Home", "Index", "Home", new { #class = "dropdown-item" })
#Html.ActionLink("About", "About", "Home", new { #class = "dropdown-item" })
#Html.ActionLink("Contact", "Contact", "Home", new { #class = "dropdown-item" })
</div>
</li>
</ul>
</div>
</div>
</nav>
When on the Home Page using the home controller the navbar is converted correctly in the the browser
<div class="navbar-nav mr-auto">
<ul class="navbar-nav nav">
<li class="nav-item">
<a class="nav-link" href="/home/Index?Length=4">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Home/About?Length=4">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Home/Contact?Length=4">Contact</a>
</li>
<li class="dropdown">
Dropdown
<div class="dropdown-menu">
<a class="dropdown-item" href="/home/Index?Length=4">Home</a>
<a class="dropdown-item" href="/Home/About?Length=4">About</a>
<a class="dropdown-item" href="/Home/Contact?Length=4">Contact</a>
</div>
</li>
</ul>
</div>
But when I change to Active user page the navbar is not converted correctly. The homecontroller is replaced with the ActiveUser controller which is not what it should be doing.
<div class="navbar-nav mr-auto">
<ul class="navbar-nav nav">
<li class="nav-item">
<a class="nav-link" href="/ActiveUser/index?Length=4">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/ActiveUser/About?Length=4">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/ActiveUser/Contact?Length=4">Contact</a>
</li>
<li class="dropdown">
Dropdown
<div class="dropdown-menu">
<a class="dropdown-item" href="/ActiveUser/index?Length=4">Home</a>
<a class="dropdown-item" href="/ActiveUser/About?Length=4">About</a>
<a class="dropdown-item" href="/ActiveUser/Contact?Length=4">Contact</a>
</div>
</li>
</ul>
</div>
Any help will be greatly appreciated.
I finally found the answer to the one part of the question on Stackoverflow at these posts:
What is the purpose of `area = "" ` in an Html.ActionLink?
How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas

Sidebar menu hide from users using laravel

I am a beginner at laravel and I have Hidden sidebar menu from users it's successfully working but when logout from admin then I face error how to fix it thanks. please see error https://flareapp.io/share/NPL9bz7w
Html view
<!-- Sidebar Menu -->
<nav class="mt-2">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview"
role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->
<!--Admin sidebar menu -->
#if(Auth::user()->type == "admin")
<li class="nav-item">
<a href="{{url('admin')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p>Users Permission</p>
</a>
</li>
<li class="nav-item">
<a href="{{url('manage_users')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p>Manage Users</p>
</a>
</li>
<li class="nav-item">
<a href="{{url('register')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p> User Register</p>
</a>
</li>
<!-- end Admin sidebar menu -->
<!-- User sidebar menu -->
#else
<li class="nav-item">
<a href="{{url('viewprofile')}}" class="nav-link">
<i class="fas fa-user"> </i>
<p>View Profile</p>
</a>
</li>
<li class="nav-item">
<a href="{{url('DashBoard')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p>Chat Room</p>
</a>
</li>
#endif
</ul>
</nav>
<!-- /.sidebar-menu -->
The problem is, you are checking #if(Auth::user()->type == "admin") for check the logged in admin. If the admin is logged out then there is no Auth::user(). I think this is the problem you are facing. So change the condition like this
#if(Auth::check() && Auth::user()->type == "admin")
// Admin menu bar
#elseif (Auth::check())
// User menu bar
#endif
I think this will solve the issue.
Since the user is logged out, the Auth::user() is null.
Because of which, when you try to get its type, it is throwing
Trying to get property 'type' of non-object
You need to check whether the user is logged in, and then check its type.
#if(Auth::check() && Auth::user()->type == "admin")
Auth::check() will return true if the user is logged in
first check if user logged in then check that if user is admin or not:
<!-- Sidebar Menu -->
<nav class="mt-2">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview"
role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->
<!--Admin sidebar menu -->
#auth
#if(Auth::user()->type == "admin")
<li class="nav-item">
<a href="{{url('admin')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p>Users Permission</p>
</a>
</li>
<li class="nav-item">
<a href="{{url('manage_users')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p>Manage Users</p>
</a>
</li>
<li class="nav-item">
<a href="{{url('register')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p> User Register</p>
</a>
</li>
<!-- end Admin sidebar menu -->
<!-- User sidebar menu -->
#else
<li class="nav-item">
<a href="{{url('viewprofile')}}" class="nav-link">
<i class="fas fa-user"> </i>
<p>View Profile</p>
</a>
</li>
<li class="nav-item">
<a href="{{url('DashBoard')}}" class="nav-link">
<i class="fas fa-comments"></i>
<p>Chat Room</p>
</a>
</li>
#endif
#endauth
</ul>
use isset method
#if(isset(Auth::user()->type) && Auth::user()->type== "admin")
you can also use Auth::check()
#if(Auth::check() && Auth::user()->type== "admin")

Laravel submit form from a tab and return to same tab in view

I have a view with multiple tabs. Each having different forms. When I submit a form from one tab, it returns to same page but primary tab. How could I get to return to same tab from which I was working.
Controller
public function recieve(Request $request)
{
$item = Item::find($request->input('item'));
$item->recieved_at = now();
$item->recieved_status = "1";
$item -> save();
return redirect('/files/'.$item->file)->with('success','Item Recieved Confirmed');
}
view - tabs
<ul class="nav nav-tabs" id="fileTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="profile-tab" data-toggle="tab" href="#profile" role="tab"
aria-controls="profile" aria-selected="true">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" id="job-tab" data-toggle="tab" href="#job" role="tab" aria-controls="job"
aria-selected="false">Job</a>
</li>
<li class="nav-item">
<a class="nav-link" id="items-tab" data-toggle="tab" href="#items" role="tab" aria-controls="items"
aria-selected="false">Items</a>
</li>
<li class="nav-item"><a class="nav-link" id="mechanic-tab" data-toggle="tab" href="#mechanic" role="tab"
aria-controls="mechanic" aria-selected="false">Labour Hours</a>
</li>
<li class="nav-item">
<a class="nav-link" id="accounts-tab" data-toggle="tab" href="#accounts" role="tab"
aria-controls="accounts" aria-selected="false">Accounts</a>
</li>
<li class="nav-item">
<a class="nav-link" id="accounts-tab" data-toggle="tab" href="#preview" role="tab"
aria-controls="accounts" aria-selected="false">Print Preview</a>
</li>
</ul>
Form
#if($file->job_status == "Closed")#else
{!! Form::open(['action' => 'FilesController#item','method' => 'POST']) !!}
<div class="row pt-3 pb-1">
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::text('part_number','',['class'=>'form-control','placeholder'=>'Part Number'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-6">
{{Form::text('description','',['class'=>'form-control','placeholder'=>'Part Name'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::text('qty','',['class'=>'form-control','placeholder'=>'Qty'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::select('recieved_by',$users,null,['class'=>'form-control'])}}
</div>
</div>
{{Form::hidden('file',$file->id)}}
{{Form::submit('Release Item',['class'=>'form-control btn-danger btn btn-sm'])}}
{!! Form::close() !!}
#endif
I tried using hashtag tab name in return redirect statement,
return redirect('/files/'.$item->file."#items-tab")->with('success','Item Recieved Confirmed');
it would simply highlight the name but would not select. How could I achieve it?
your tab navigation,
<ul class="nav nav-tabs" id="tabMenu" role="tablist">
<li><i class="fa fa-camera" aria-hidden="true"></i> Galleries</li>
</ul>
hidden input field for tab
<input type="hidden" name="tab" value="Galleries">
RedirectRoute
$tab = $request->get('tab');
return back()->withInput(['tab'=>$tab]);
Javascript,
<script>
//redirect to specific tab
$(document).ready(function () {
$('#tabMenu a[href="#{{ old('tab') }}"]').tab('show')
});
</script>
If you want to do it in laravel than all you can do is,
-Give an id to each tab and an write an active class.
-Put a hidden input with value of the parent tab id.
-So, laravel will receive the tab id in the request object.
-Now, you can do whatever and return the tab id with to the view.
-Give a if condition to each tab item and check if matches the id from laravel response.
The match the id set the active class to it.
example
<ul>
<li id='tab1 {{ session()->get('tabId') === 'tab1' ? 'active' : '' }}'></li>
<li id='tab2 {{ session()->get('tabId') === 'tab2' ? 'active' : '' }}'></li>
</ul>
// tab1 body
<form>
<input type="hidden" name="tabId" value="tab1">
...
</form>
// tab2 body
<form>
<input type="hidden" name="tabId" value="tab2">
...
</form>
// controller
public funciton(Request $request) {
//tabId
$tabId = $request->input('tabId')
....
....
return redirect()->back()->with('tabId' => $tabId);
}
You can do this:
Controller:
public function tab() {
$active_tab = "tab2";
return view('tabs.index', compact('active_tab'));
}
View:
<ul class="nav nav-tabs">
<li class="#if($active_tab=="tab1") active #endif"><a data-toggle="tab" href="#home">Home</a></li>
<li class="#if($active_tab=="tab2") active #endif"><a data-toggle="tab" href="#menu1">Menu 1</a></li>
<li class="#if($active_tab=="tab3") active #endif"><a data-toggle="tab" href="#menu2">Menu 2</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade #if($active_tab=="tab1") in active #endif">
<h3>HOME</h3>
<p>Some content.</p>
</div>
<div id="menu1" class="tab-pane fade #if($active_tab=="tab2") in active #endif">
<h3>Menu 1</h3>
<p>Some content in menu 1.</p>
</div>
<div id="menu2" class="tab-pane fade #if($active_tab=="tab3") in active #endif">
<h3>Menu 2</h3>
<p>Some content in menu 2.</p>
</div>
</div>
You can show a specific tab with this code:
function showTab(tab){
$('.nav-tabs a[href="#' + tab + '"]').tab('show');
};
// Verify location.hash
if (window.location.hash !== ''){
showTab(window.location.hash);
}
But I would pass a query string in the return, just to make sure there will be no collision with other eventual hashes. Something like:
return redirect('/files/'.$item->file."?openTab=your-tab-id")->with('success','Item Recieved Confirmed');
Of course, you would change the "location.hash" verification to a query string verification. Example: https://davidwalsh.name/query-string-javascript

VueJS get variable as text

I'm working on a Laravel project and i use VueJS for notifications system.
However, i'm a real noob in VueJS dev'. Consequently, i don't know how to print a variable as text.
Situation : System of notifications work and i would like to print the number of notifications in a bootsrap badge. So, i did this :
<i class="fa fa-bell-o" aria-hidden="true"></i> Notifications <span id='badge' class="badge badge-info">{{notifications.length}}</span> <span class="caret"></span>
With this code in my Notification.vue file :
<template>
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
<i class="fa fa-bell-o" aria-hidden="true"></i> Notifications <span id='badge' class="badge badge-info">{{notifications.length}}</span> <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li style="text-align:center" v-for="notification in notifications">
<a href="#" v-on:click="MarkAsRead(notification)">
{{notification.data.idea.name}} a été validée !<br>
</a>
</li>
<li style="text-align: center" v-if="notifications.length==0">
Aucune notification !
</li>
</ul>
</li>
</template>
<script>
export default {
props: ['notifications'],
methods: {
MarkAsRead: function (notification) {
var data = {
id: notification.id
};
axios.post('/notification/read', data).then(response => {
window.location.href = "/manif/";
});
}
}
}
</script>
Finally i only see {{notifications.length}} write in the badge instead of a number...
Can you help me about this ?
Thank's
The raw mustache tags are shown because you've used v-pre directive in the parent element, which tells Vue to not compile the element and its children:
<a id="navbarDropdown" ... v-pre> <!-- Don't use v-pre here -->
...
<span ...>{{notifications.length}}</span>
</a>
Simply removing that directive should resolve the issue: demo

Resources