简体   繁体   中英

How to check if user is logged on, Not Member?

Have tried 2 different methods to check if the user is logged into Umbraco Administration from the front-end in a User Controls Code-Behind (.cs) file:

Page.User.Identity.IsAuthenticated

and

umbraco.library.IsLoggedOn()

Both of these always returns false, even when I am logged in. How to check if user is logged in correctly?? Trying to get this on the front-end. Is there a way to do this on the front-end? Using Umbraco Version 4.6.1.

Furthermore, In the Administration of Umbraco, these are Users, not Members! So, I need to know if a User is logged into Umbraco, not Member(s)!

Nevermind, figured it out...

private bool _IsAdminLoggedIn = false;

if (umbraco.helper.GetCurrentUmbracoUser() != null)
{
    _IsAdminLoggedIn = !string.IsNullOrEmpty(umbraco.helper.GetCurrentUmbracoUser().UserType.Alias) && umbraco.helper.GetCurrentUmbracoUser().UserType.Alias == "admin";
}

Now _IsAdminLoggedIn returns true or false depending if the UserType "admin" is logged into Umbraco or not!

Cheers :)

umbraco.helper.GetCurrentUmbracoUser() did not work for me in Umbraco version 7.3.5 so I used this instead:

private bool _IsAdminLoggedIn = false;

var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
if (userTicket != null)
{
    var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
    if (!String.IsNullOrEmpty(currentUser.UserType.Alias) && currentUser.UserType.Alias == "admin")
    {
    _IsAdminLoggedIn = true
    }
}

Some of the other examples use obsolete methods of retrieving the user type. Here is a more up-t-date method to check the user is logged in and is an admin user.

var userTicket = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
if (userTicket != null)
{
    var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
    if (!currentUser.Groups.Any(x => x.Alias.Equals("admin")))
    {
        // Do something if the user is not an admin
        Response.Redirect("~/");
    }
}

在 Umbraco 7.7 我使用Security.CurrentUser

_IsAdminLoggedIn = Security.CurrentUser.UserType.Alias == "admin";

I made an extension method:

public static class HttpContextExtensions
{
    public static bool IsUserLoggedInUmbraco(this HttpContext httpContext)
    {
        if (httpContext.User == null) return false;
        if (httpContext.User.Identity is UmbracoBackOfficeIdentity) return true;

        var userTicket = new HttpContextWrapper(httpContext).GetUmbracoAuthTicket();
        if (userTicket == null) return false;

        if (!UmbracoContext.Current.HttpContext.AuthenticateCurrentRequest(userTicket, renewTicket: false)) return false;
        return httpContext.User.Identity is UmbracoBackOfficeIdentity;
    }
}

And you use it like:

var isLoggedIn = HttpContext.Current.IsUserLoggedInUmbraco();

The System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket method mentioned by Ogglas seems to still work in Umbraco v8.6, but I kept getting errors on GetUmbracoAuthTicket() until I added this using statement:

@using Umbraco.Web.Security;

So a full solution in a Razor view would be:

@using Umbraco.Web.Security;
    
@{
    var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
    if (userTicket == null)
    {
        // Redirect to authentication or other
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM