Reputation: 1650
I am using the default MVC 6 template in Visual Studio. I need to check if a user is authenticated in a class. This should be very simple but I cannot figure out why its not working. Thanks in advance.
using Microsoft.AspNet.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Security.Claims;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Security.Principal;
namespace fake.Classes
{
public class fakeUser
{
public static string getBrand()
{
if (User.Identity.IsAuthenticated)
{
return getThemeName();
}
}
}
}
Upvotes: 3
Views: 7068
Reputation: 5634
For the ASP.NET Core 1 version, you need to inject SignInManager to your controller via the consutructor, add that to a private signInManager field.
private SignInManager<ApplicationUser> _signInManager;
constructor:
SignInManager<ApplicationUser> signInManager
_signInManager = signInManager;
then, you can use the signinmanager to check authentication:
_signInManager.IsSignedIn(User)
Upvotes: 3