Partner-Center-DotNet-Samples icon indicating copy to clipboard operation
Partner-Center-DotNet-Samples copied to clipboard

Support of .Net 5

Open SibeeshVenu opened this issue 5 years ago • 5 comments

As you know that the ".Net 5" had been released on November 10th, 2020, I would like to know whether the official Microsoft.Store.PartnerCenter SDK supports ".Net 5" or not? Has anyone already tried this? Is there a Github repository for this SDK?

Mentioning you @isaiahwilliams here, as you closed this issue.

SibeeshVenu avatar Nov 11 '20 13:11 SibeeshVenu

Here it is mentioned that the SDK will support .Net Core 2 and above, however, it would be great if anyone confirms it. And from this issue I can see that from".Net 5" we will be using the Azure AD v2.0 endpoints (Microsoft.Identity.Web). Does this have any effects on the Partner Center SDK?

SibeeshVenu avatar Nov 11 '20 15:11 SibeeshVenu

Hi, I we are using PartnerCenter SDK 1.16.3 with Azure Functions and .NET Core 3.1. We are also using MSAL (Microsoft.Identity.Client) for Authentication with a small wrapper around an AuthenticationResult. Make sure you use the Scopes "https://api.partnercenter.microsoft.com/user_impersonation" when getting tokens from MSAL. Also, we implemented the secure access model using SetBeforeAccessAsyncand setAfterAccessAsyncof the ITokenCache of MSAL.

    public class IdentityClientPartnerCredentials : IPartnerCredentials
    {
        private readonly AuthenticationResult _authResult;
        public IdentityClientPartnerCredentials(AuthenticationResult authResult)
        {
            _authResult = authResult;
        }
        public bool IsExpired() => ExpiresAt <= DateTimeOffset.UtcNow;
        public string PartnerServiceToken => _authResult.AccessToken;
        public DateTimeOffset ExpiresAt => _authResult.ExpiresOn.AddMinutes(-1);
    }
public class KeyVaultTokenCaches
    {
        private readonly HashSet<string> _syncedCaches = new HashSet<string>();
        private readonly SecretClient _secretClient;

        public KeyVaultTokenCaches(string keyVaultName)
        {
            var kvUri = "https://" + keyVaultName + ".vault.azure.net";
            _secretClient = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
        }
        
        public void Initialize(ITokenCache tokenCache)
        {
            tokenCache.SetBeforeAccessAsync(BeforeAccessAsyncHandler);
            tokenCache.SetAfterAccessAsync(AfterAccessAsyncHandler);
        }


        public async Task AfterAccessAsyncHandler(TokenCacheNotificationArgs arg)
        {
            if (!arg.HasStateChanged)
                return;
            await _secretClient.SetSecretAsync(arg.SuggestedCacheKey.Replace(".", "X"), Encoding.UTF8.GetString(arg.TokenCache.SerializeMsalV3()));
            _syncedCaches.Add(arg.SuggestedCacheKey);
        }

        public async Task BeforeAccessAsyncHandler(TokenCacheNotificationArgs arg)
        {
            if (_syncedCaches.Contains(arg.SuggestedCacheKey))
                return;
            try
            {
                var data = await _secretClient.GetSecretAsync(arg.SuggestedCacheKey.Replace(".", "X"));
                if(data is null || string.IsNullOrEmpty(data.Value.Value))
                    return;
                var cache = Encoding.UTF8.GetBytes(data.Value.Value);
                arg.TokenCache.DeserializeMsalV3(cache);
                _syncedCaches.Add(arg.SuggestedCacheKey);
            }
            catch (Azure.RequestFailedException err)
            {
                Console.WriteLine(err.GetType().ToString());
            }
        }
    }
_app = ConfidentialClientApplicationBuilder.Create(clientId)
    .WithClientSecret(clientSecret)
    .WithTenantId(tenantId)
    Build();
_tokenCaches = new KeyVaultTokenCaches(keVaultName);
_tokenCaches.Initialize(_app.UserTokenCache);
string[] Scopes = {"https://api.partnercenter.microsoft.com/user_impersonation"};
var account = await _app.GetAccountAsync(storedAdminAccountIdentifier);
var token = await _app.AcquireTokenSilent(Scopes, account).ExecuteAsync();

claasd avatar Mar 08 '21 20:03 claasd

It seems we can't use it yet due to dependency issues with EntityFrameworkCore 5.x image

ThomasPe avatar Mar 12 '21 14:03 ThomasPe

It seems we can't use it yet due to dependency issues with EntityFrameworkCore 5.x

That just means you can't use Entity Framework 5 in the same project. Don't use it or move it to a separate project. This is the same with Azure Functions.

ronaldpschutte avatar Jun 01 '21 16:06 ronaldpschutte

When using this with an .Net 5 api project I get warnings, can the System.ComponentModel.Annotation version be updated?

Detected package version outside of dependency constraint: Microsoft.Store.PartnerCenter 2.0.1 requires System.ComponentModel.Annotations (= 4.7.0) but version System.ComponentModel.Annotations 5.0.0 was resolved.

ErikdeWilde avatar Sep 29 '21 07:09 ErikdeWilde