我已在 azure 门户
中创建了 Azure SignalR(无服务器)资源
。
然后,我在本地创建了一个引用 Microsoft.Azure.WebJobs.Extensions.SignalRService 的 Azure 函数 HttpTrigger。在我的azure函数
中,我有以下代码:
`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 应用程序项目
。所以在这里我像这样连接到这个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 允许的来源,我添加了我的客户端本地主机应用程序。
最佳答案
关闭这个是因为我找到了答案。我错过了这个,这真的很烦人。
我将 AuthorizationLevel.Function
替换为 AuthorizationLevel.Anonymous
。因为我只是传递我的 azure 函数的域/api 并让 signalR 在它们的 JS 上执行其操作。
关于asp.net - Azure signalR + Azure http 触发器 - 部署时授权错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74373320/