Update 2020/12/22
I have checked the source code of all major packages, there is no way to create the access policy based on namespace with those packages, there only can create on entities.
So, let follow a more basic way, use rest API(The underly logic of those packages is also based on this.).
Below is the code and works fine on my side:
using Microsoft.Azure.Services.AppAuthentication;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp23
{
class Program
{
static void Main(string[] args)
{
CallWebAPIAsync().Wait();
}
static async Task CallWebAPIAsync()
{
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").Result;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
client.BaseAddress = new Uri("https://management.azure.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//PUT Method
var jsonString = "{ \"properties\": { \"rights\": [ \"Listen\", \"Send\", \"Manage\" ] } }";
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PutAsync("https://management.azure.com/subscriptions/e5b0xxxxxx9f4c68/resourceGroups/0730BowmanWindow/providers/Microsoft.ServiceBus/namespaces/bowman1012/AuthorizationRules/testbowman?api-version=2017-04-01",httpContent);
if (response.IsSuccessStatusCode)
{
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}
else
{
Console.WriteLine("Internal server Error");
}
}
}
}
}
It works fine:
Below is the rest API reference, any doubts please let me know.
https://docs.microsoft.com/en-us/rest/api/servicebus/stable/namespaces%20-%20authorization%20rules/createorupdateauthorizationrule
Update:
.csproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.0.0" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.1.0" />
</ItemGroup>
</Project>
Program.cs
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Primitives;
using Azure.Messaging.ServiceBus.Administration;
using System;
using System.Text;
namespace ConsoleApp15
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
string keyname = "test";
string sharedaccesskey = "xxxxxx";
var validityDuration = TimeSpan.FromHours(24);
TokenScope tokenScope = TokenScope.Entity;
//Get the SAS based on the namespace.
var tokenprovider =(SharedAccessSignatureTokenProvider) TokenProvider.CreateSharedAccessSignatureTokenProvider(keyname,sharedaccesskey,validityDuration,tokenScope);
var token =await tokenprovider.GetTokenAsync("sb://bowman1012.servicebus.windows.net/", TimeSpan.FromHours(24));
//Send message to service bus queue based on SAS token.
ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder {
Endpoint= "sb://bowman1012.servicebus.windows.net/",
EntityPath= "testbowman",
SasToken= token.TokenValue
};
QueueClient client = new QueueClient(builder);
Message mes = new Message();
mes.Body = Encoding.ASCII.GetBytes("This is a test.20201208");
await client.SendAsync(mes);
//Create a topic with 5120MB.
var client1 = new ServiceBusAdministrationClient("Endpoint=sb://bowman1012.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx");
var topicOptions = new CreateTopicOptions("20201208")
{
AutoDeleteOnIdle = TimeSpan.FromDays(7),
DefaultMessageTimeToLive = TimeSpan.FromDays(2),
DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1),
EnableBatchedOperations = true,
EnablePartitioning = false,
MaxSizeInMegabytes = 5120,
RequiresDuplicateDetection = true,
UserMetadata = "some metadata"
};
topicOptions.AuthorizationRules.Add(new SharedAccessAuthorizationRule(
"allClaims",
new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen }));
await client1.CreateTopicAsync(topicOptions);
Console.WriteLine("Hello World!" + "\n" + token.TokenValue);
}
}
}
And works fine on my side:
Original Answer:
How can I create SAS policy in azure service bus namespace?
You can use SharedSecretTokenProvider.BuildKey(string appliesTo, string action)
method to create the SAS.
This is the API reference:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.sharedaccesssignaturetokenprovider?view=azure-dotnet
Or you can refer to this doc(some simple code):
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-sas#use-shared-access-signature-authorization
There is any way to configure topic/subscriptions while creating?
(because while I create topic using above given code it create topic
with Max size 1024 MB but I need to set it 5120 MB)?
Use NamespaceManager.CreateTopic method, this method returns TopicDescription
object, you can set the size by the MaxSizeInMegabytes property(in megabytes).
This is the API reference:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.namespacemanager.createtopic?view=azure-dotnet
https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.topicdescription.maxsizeinmegabytes?view=azure-dotnet#Microsoft_ServiceBus_Messaging_TopicDescription_MaxSizeInMegabytes
Apart from this there is any better way(best practice) to create topic, subscription and SAS policy using connection string in .Net Core 2.1?
I'm not sure what you are worried about on this point, maybe you can explain what you are looking for? This method of obtaining is provided by the above official document.
And the method to create subscription:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.namespacemanager.createsubscription?view=azure-dotnet