Add extension method for getting custom user properties
Closed this issue · 2 comments
User @rickers1 asked:
I’m really looking for an extension that allows me to do something like (C#)
var companyName = User.GetCustomPropertyValue(“company_name”);
@StewartBellamy one for you or Jim
I don't think this approach would be correct, since the KVP system is built upon a typical .Net Core structure of DI-injected services. Extension methods are inherently static, with no constructor to DI-inject the underlying data services that query the KVP.
However you could DI-inject an instance of IUserPropertyService
@using cloudscribe.UserProperties.Models;
@inject IUserPropertyService kvps
and I will add a simple method:
public async Task<string> FetchUserProperty(string userId, string key)
so (in a view) you could just say:
<h1>@await kvps.FetchUserProperty(User.GetUserId(), "PostCode")</h1>
(Will be available in a couple of days time once the latest NuGets are tested and pushed).