Get current member in content finder
# help-with-umbraco
p
I think I am going crazy, but how can I get the current member in a content finder? I want to determine if the user has access to the given content or not. I have tried: - Injecting IMemberManager, but not possible due to the lifecycle - Inject IScopeProvider and creating a new scope, but that just returns a NULL member - Getting the current context and getting the user/identity, but that also returns NULL
a
Do you have an example of your code? At the very least, you should be able to get the username (email) of the current member via
Context.User.Identity.Name
where
Context
is the current
HttpContext
. Or is that what you're mentioning that is
null
? There may also be multiple identities, but I think members would be the default one.
p
Yeah, so this is kind of what I would expect to work:
.User has a single identity, but all the properties of that Identity are NULL. Though if I check the Context.User in the view, it is populated
Almost like it gets populated after the content finders, which would be a bit strange
a
Yes, sounds a bit strange if that is the case. Is the view that you're mentioning accessed as a result of the content finder?
d
If I remember correctly, routing takes place before authentication in the middleware pipeline (because you need to know where you're going before you know if you need to authenticate or not), so I wouldn't be surprised if that is why you can't access the user in a content finder
p
Yep
That would explain this behavior yeah. Maybe I have to work around it a little bit and move that code to either a controller or the view itself
a
It may also be possible to handle the authentication on your own. I'm doing that to validate the backoffice user from a content finder
p
Do you have an example? Not quite sure how to do that if Umbraco hasn't pushed it to the Identity yet
a
You can call this extension method from your content finder to authenticate the backoffice user from the saved cookie and then return the claims identity. Not really sure how that tranfers to members. https://github.com/skybrud/Skybrud.Essentials.Umbraco/blob/v2/main/src/Skybrud.Essentials.Umbraco/HttpContextExtensions.cs#L33-L44
p
Super cool 🙌 Will give that a try for members! Thank you!
3 Views