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

廉价感情. 提交于 2021-01-21 06:06:09

问题


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)!


回答1:


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 :)




回答2:


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
    }
}



回答3:


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("~/");
    }
}



回答4:


In Umbraco 7.7 I'm using Security.CurrentUser

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



回答5:


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();



回答6:


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
    }
}


来源:https://stackoverflow.com/questions/25476810/how-to-check-if-user-is-logged-on-not-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!