Azure signalR + Azure http触发器-部署时授权错误



我在azure portal中创建了一个Azure SignalR (Serverless) reosurce。然后我在本地创建了一个引用Microsoft.Azure.WebJobs.Extensions.SignalRServiceazure function HttpTrigger。在我的azure function中,我有这个代码:

`public static class HttpTrigger
{
[FunctionName("Negotiate")]
public static SignalRConnectionInfo Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[SignalRConnectionInfo(HubName = "notificationHub")] SignalRConnectionInfo connectionInfo,
ILogger log)
{
log.LogInformation("Returning connection: " + connectionInfo.Url + "" + connectionInfo.AccessToken);
return connectionInfo;
}
[FunctionName("Notify")]
public static async Task<IActionResult> Notify([HttpTrigger(AuthorizationLevel.Function, "get", Route=null)] HttpRequest req,
[SignalR(HubName = "notificationHub")] IAsyncCollector<SignalRMessage> signalRMessage,
ILogger log)
{
log.LogInformation("Notify");
string msg = string.Format("Message from agent! {0} ", DateTime.Now);
await signalRMessage.AddAsync(
new SignalRMessage
{
Target = "notifications",
Arguments = new[] { msg }
});
return new OkObjectResult("ok");
}
}

'同样在我的azure函数中,这是我的local.settings.json的样子:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureSignalRConnectionString": "myconnstringhere"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "http://localhost:53377",
"CORSCredentials": true
}
}

为了解决CORS问题,我在我的客户端部分项目中添加了http://localhost:53377域。

我的客户端部分是单独的asp.net web application project。这里我像这样连接到这个azure函数:

<script>
$(document).ready(function(){
const connection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:7071/api/")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.onclose(start);
start(connection);
});

async function start(connection){
try {
await connection.start();
console.log("SignalR connected.");
connection.on("notifications", (message) => {
$("#detailcontainer").html(message);
console.log(message)
});
}
catch(err) {
console.log(err);
}
}
</script>
现在,我已经发布了我的azure函数。但现在它不再起作用了。当触发/api/negotiate时,会得到一个错误,说未经授权。

我的azure功能是一个.net 6项目,而客户端应用程序是一个net framework 4.8。这是因为我的客户端应用程序仍然在webforms吗?

我已经将我的azure signalR的连接字符串添加到应用程序设置中,其名称格式如下:Azure__SignalR__ConnectionString我还为我的azure功能配置了CORS允许的起源,我添加了我的客户端localhost应用程序。

关闭这个,因为我找到了答案。我错过了这一点,这真的很烦人。

我将AuthorizationLevel.Function替换为AuthorizationLevel.Anonymous。因为我只是传递我的azure函数的域/api,让signalR在JS上做它的事情。

最新更新