Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase C#Blazor WASM | Firestore:使用Google.Cloud.Firestore.FirestoreDb.CreateAsync时接收混合内容错误_Firebase_Google Cloud Firestore_Blazor_Blazor Client Side - Fatal编程技术网

Firebase C#Blazor WASM | Firestore:使用Google.Cloud.Firestore.FirestoreDb.CreateAsync时接收混合内容错误

Firebase C#Blazor WASM | Firestore:使用Google.Cloud.Firestore.FirestoreDb.CreateAsync时接收混合内容错误,firebase,google-cloud-firestore,blazor,blazor-client-side,Firebase,Google Cloud Firestore,Blazor,Blazor Client Side,尝试在C#(Blazor WASM)中创建FirebaseDb对象会在部署到Firebase后引发阻止的混合内容错误。 有没有办法强制使用HTTPS 错误是: 已阻止加载混合活动内容“” dotnet.3.2.0-preview3.20168.1.js:1:163131 crit:Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]blazor.WebAssembly.js:1:36074

尝试在C#(Blazor WASM)中创建FirebaseDb对象会在部署到Firebase后引发阻止的混合内容错误。 有没有办法强制使用HTTPS

错误是:

已阻止加载混合活动内容“” dotnet.3.2.0-preview3.20168.1.js:1:163131 crit:Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]blazor.WebAssembly.js:1:36074 尝试获取资源时出现未处理的异常呈现组件:TypeError:NetworkError。blazor.webassembly.js:1:36074 WebAssembly.JSException:TypeError:尝试获取资源时出现NetworkError。blazor.webassembly.js:1:36074 在0 blazor.WebAssembly.js:1:36074中的WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler.doFetch(System.Threading.Tasks.TaskCompletionSource
1[TResult]tcs,System.Net.Http.HttpRequestMessage request,System.Threading.CancellationToken CancellationToken)
在0 blazor.WebAssembly.js:1:36074中的WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler.sendsync(System.Net.Http.HttpRequestMessage请求,System.Threading.CancellationToken CancellationToken)处
在0 blazor.webassembly.js:1:36074中的System.Net.Http.HttpClient.FinishSendAsyncBuffered(System.Threading.Tasks.Task
1[TResult]sendTask、System.Net.Http.HttpRequestMessage请求、System.Threading.CancellationTokenSource cts、System.Boolean Dispositions)中 在0 blazor.webassembly.js:1:36074中的Google.api.Auth.OAuth2.ComputeCredential.isrunning计算机引擎缓存()中 在0 blazor.webassembly.js:1:36074中的Google.api.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialAsync()中 位于:0 blazor.webassembly.js:1:36074中的Google.Api.Gax.Grpc.ChannelPool.CreateChannelCredentialsUncached() 位于:0 blazor.webassembly.js:1:36074中的Google.Api.Gax.Grpc.ChannelPool.GetChannelAsync(Google.Api.Gax.Grpc.ServiceEndpoint端点,System.Collections.Generic.IEnumerable`1[T]channelOptions) 位于:0 blazor.webassembly.js:1:36074中的Google.Cloud.Firestore.V1.FirestoreClient.CreateAsync(Google.Api.Gax.Grpc.ServiceEndpoint端点,Google.Cloud.Firestore.V1.FirestoreSettings设置) 位于0 blazor.webassembly.js:1:36074中的Google.Cloud.Firestore.FirestoreDb.CreateAsync(System.String projectId,Google.Cloud.Firestore.V1.FirestoreClient) 在blog.Pages.Index.OnInitializedAsync()中:0 blazor.webassembly.js:1:36074 位于:0 blazor.webassembly.js:1:36074中的Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() 位于:0 blazor.webassembly.js:1:36074中的Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(System.Threading.Tasks.Task taskToHandle)

守则:

    protected override async Task OnInitializedAsync()
    {
        string projectId = "my-poject-id";
        FirestoreDb db = await FirestoreDb.CreateAsync(projectId);
    }

TL;DR
Firestore库不应在浏览器上下文中运行。它被开发用于服务器端或管理工具中。该库将允许您在Firestore中执行所有操作,并且将凭据发送给客户将存在安全风险


根据stacktrace,我假设您使用的是
Google.Cloud.Firestore
库。 不幸的是,此库被设计为一个“”。要在服务器端使用的客户端库。
这些“服务器客户端库”的设计与“.”不同。
移动/网络库将使用Firebase的身份验证(用户名/密码、Facebook等),安全模型将应用于该用户上下文。 服务器库本质上让您可以访问所有内容

现在,您遇到的问题是由Blazor不支持此SDK引起的。 首先,SDK所依赖的
GOOGLE_APPLICATION_CREDENTIALS
环境变量在Blazor应用程序中不可用。 如果该文件可用,则无法从浏览器沙盒中访问该文件

通过使用
FirestoreDbBuilder
并手动设置
JsonCredentials
ProjectId
可以避免这两个问题:

var builder=new FirestoreDbBuilder();
builder.JsonCredentials=“{\”类型\“:\”服务\帐户\“,\”项目\ id \“:\”,\“私钥\ id\”:\”,\“私钥\”:\”,“客户端\电子邮件\“:\”,“客户端\ id\”:“\”,“身份验证uri\:\”https://accounts.google.com/o/oauth2/auth\“,\“令牌uri\”:\”https://oauth2.googleapis.com/token\“,\“身份验证提供程序\u x509\u证书\u url\”:\”https://www.googleapis.com/oauth2/v1/certs\“,\“客户端\u x509\u证书\u url\”:\“\”}”;
builder.projectd=“”;
FirestoreDb=builder.Build();
这将导致下一个问题,所使用的GRPC库不支持Blazor/.NET WASM,导致此stacktrace:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Unsupported platform.
System.InvalidOperationException: Unsupported platform.
  at Grpc.Core.Internal.NativeExtension.GetNativeLibraryFilename () [0x0003f] in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\NativeExtension.cs:231 
  at Grpc.Core.Internal.NativeExtension.LoadUnmanagedLibrary () [0x0000a] in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\NativeExtension.cs:93 
  at Grpc.Core.Internal.NativeExtension.LoadNativeMethods () [0x0001a] in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\NativeExtension.cs:120 
  at Grpc.Core.Internal.NativeExtension..ctor () [0x00006] in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\NativeExtension.cs:40 
  at Grpc.Core.Internal.NativeExtension.Get () [0x00022] in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\NativeExtension.cs:65 
  at Grpc.Core.Internal.NativeMethods.Get () [0x00000] in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\NativeMethods.cs:49 
  at Grpc.Core.GrpcEnvironment.GrpcNativeInit () [0x00016] in T:\src\github\grpc\src\csharp\Grpc.Core\GrpcEnvironment.cs:373 
  at Grpc.Core.GrpcEnvironment..ctor () [0x0001e] in T:\src\github\grpc\src\csharp\Grpc.Core\GrpcEnvironment.cs:302 
  at Grpc.Core.GrpcEnvironment.AddRef () [0x00028] in T:\src\github\grpc\src\csharp\Grpc.Core\GrpcEnvironment.cs:78 
  at Grpc.Core.Channel..ctor (System.String target, Grpc.Core.ChannelCredentials credentials, System.Collections.Generic.IEnumerable`1[T] options) [0x00041] in T:\src\github\grpc\src\csharp\Grpc.Core\Channel.cs:70 
  at Google.Api.Gax.Grpc.GrpcCore.GrpcCoreAdapter.CreateChannelImpl (System.String endpoint, Grpc.Core.ChannelCredentials credentials, Google.Api.Gax.Grpc.GrpcChannelOptions options) [0x00000] in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc.GrpcCore\GrpcCoreAdapter.cs:34 
  at Google.Api.Gax.Grpc.GrpcAdapter.CreateChannel (System.String endpoint, Grpc.Core.ChannelCredentials credentials, Google.Api.Gax.Grpc.GrpcChannelOptions options) [0x00024] in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\GrpcAdapter.cs:29 
  at Google.Api.Gax.Grpc.ClientBuilderBase`1[TClient].CreateChannel (System.String endpoint, Grpc.Core.ChannelCredentials credentials) [0x00000] in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ClientBuilderBase.cs:423 
  at Google.Api.Gax.Grpc.ClientBuilderBase`1[TClient].CreateCallInvokerAsync (System.Threading.CancellationToken cancellationToken) [0x00145] in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ClientBuilderBase.cs:313 
  at Google.Cloud.Firestore.V1.FirestoreClientBuilder.BuildAsyncImpl (System.Threading.CancellationToken cancellationToken) [0x00033] in /_/apis/Google.Cloud.Firestore.V1/Google.Cloud.Firestore.V1/FirestoreClient.g.cs:285 
  at Google.Cloud.Firestore.FirestoreDbBuilder.BuildAsync (System.Threading.CancellationToken cancellationToken) [0x0015a] in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/FirestoreDbBuilder.cs:119 
  at BlazorFirestore.Pages.FetchData.OnInitializedAsync () [0x00061] in C:\Users\niels\source\repos\BlazorFirestore\Pages\FetchData.razor:12 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2ff3be8 + 0x0013a> in <filename unknown>:0 
现在将
GrpcWebAdapter.Default
设置为
FirestoreDbBuilder.GrpcAdapter

var builder=new FirestoreDbBuilder();
builder.JsonCredentials=“{\”类型\“:\”服务\帐户\“,\”项目\ id \“:\”,\“私钥\ id\”:\”,\“私钥\”:\”,“客户端\电子邮件\“:\”,“客户端\ id\”:“\”,“身份验证uri\:\”https://accounts.google.com/o/oauth2/auth\“,\“令牌uri\”:\”https://oauth2.googleapis.com/token\“,\“身份验证提供程序\u x509\u证书\u url\”:\”https://www.googleapis.com/oauth2/v1/certs\“,\“客户端\u x509\u证书\u url\”:\“\”}”;
builder.projectd=“”;
builder.GrpcAdapter=GrpcWebAdapter.Default;
FirestoreDb=builder.Build();
DocumentReference docRef=db.Collection(“用户”).Document(“用户”);
字典用户=新字典
{
{“第一”,“Ada”},
{“最后一次”,“洛夫蕾丝”},
{“出生”,1815}
};
等待docRef.SetAsync(用户);
我们更进一步了,但现在我们遇到了CORS安全问题,因为这种与Firestore通信的方式不是设计用于从浏览器运行的,也没有办法将CORS与此SDK组合配置。 以下是产生的错误:

Access to fetch at 'https://firestore.googleapis.com/google.firestore.v1.Firestore/Commit' from origin 'https://localhost:5001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

dotnet.3.2.0.js:1 POST https://firestore.googleapis.com/google.firestore.v1.Firestore/Commit net::ERR_FAILED

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Status(StatusCode="Internal", Detail="Error starting gRPC call. JSException: TypeError: Failed to fetch", DebugException="WebAssembly.JSException: TypeError: Failed to fetch
        at System.Net.Http.WebAssemblyHttpHandler.doFetch (System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x3c61b60 + 0x00a30> in <filename unknown>:0 
        at System.Net.Http.WebAssemblyHttpHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x3c24040 + 0x00174> in <filename unknown>:0 
        at Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00092] in /_/src/Grpc.Net.Client.Web/GrpcWebHandler.cs:137 
        at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x3d9ae50 + 0x00134> in <filename unknown>:0 
        at Grpc.Net.Client.Internal.GrpcCall`2[TRequest,TResponse].RunCall (System.Net.Http.HttpRequestMessage request, System.Nullable`1[T] timeout) [0x0020c] in /_/src/Grpc.Net.Client/Internal/GrpcCall.cs:477 ")
Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. JSException: TypeError: Failed to fetch", DebugException="WebAssembly.JSException: TypeError: Failed to fetch
  at System.Net.Http.WebAssemblyHttpHandler.doFetch (System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x3c61b60 + 0x00a30> in <filename unknown>:0 
  at System.Net.Http.WebAssemblyHttpHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x3c24040 + 0x00174> in <filename unknown>:0 
  at Grpc.Net.Client.Web.GrpcWebHandler.SendAsyncCore (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00092] in /_/src/Grpc.Net.Client.Web/GrpcWebHandler.cs:137 
  at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x3d9ae50 + 0x00134> in <filename unknown>:0 
  at Grpc.Net.Client.Internal.GrpcCall`2[TRequest,TResponse].RunCall (System.Net.Http.HttpRequestMessage request, System.Nullable`1[T] timeout) [0x0020c] in /_/src/Grpc.Net.Client/Internal/GrpcCall.cs:477 ")
  at Google.Api.Gax.Grpc.ApiCallRetryExtensions+<>c__DisplayClass0_0`2[TRequest,TResponse].<WithRetry>b__0 (TRequest request, Google.Api.Gax.Grpc.CallSettings callSettings) [0x00051] in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ApiCallRetryExtensions.cs:27 
  at Google.Cloud.Firestore.WriteBatch.CommitAsync (Google.Protobuf.ByteString transactionId, System.Threading.CancellationToken cancellationToken) [0x000b5] in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/WriteBatch.cs:231 
  at Google.Cloud.Firestore.DocumentReference.SetAsync (System.Object documentData, Google.Cloud.Firestore.SetOptions options, System.Threading.CancellationToken cancellationToken) [0x0004b] in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/DocumentReference.cs:181 
  at BlazorFirestore.Pages.FetchData.OnInitializedAsync () [0x00166] in C:\Users\niels\source\repos\BlazorFirestore\Pages\FetchData.razor:21 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2ff3be8 + 0x0013a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x3905a50 + 0x000b6> in <filename unknown>:0 
在获取时访问'https://firestore.googleapis.com/google.firestore.v1.Firesto