我一直在测试 Blazor,但找不到任何有关如何实现浏览器缓存的文档(无论是使用图像、css、js 等静态文件,还是下载的 dll 文件)。
是否可以在 Blazor 中缓存静态文件和 dll 文件?
我正在尝试在 Blazor WASM 的 Program.cs 类中配置多个 API url。我没有看到像服务器端那样的 AddHttpClient 扩展。想知道是否有人对此有替代解决方案?
这是我到目前为止所拥有的:
var firstURI = new Uri("https://localhost:44340/");
var secondURI = new Uri("https://localhost:5001/");
void RegisterTypedClient<TClient, TImplementation>(Uri apiBaseUrl)
where TClient : class where TImplementation : class, TClient
{
builder.Services.AddHttpClient<TClient, TImplementation>(client =>
{
client.BaseAddress = apiBaseUrl;
});
}
// HTTP services
RegisterTypedClient<IFirstService, FirstService>(firstURI);
RegisterTypedClient<ISecondService, SecondService>(secondURI);
Run Code Online (Sandbox Code Playgroud) 如何在 Blazor Web assembly 中通过 HttpClient 使用 HTTP 请求标头Sec-Fetch-Mode: no-cors发出请求?
我的实际代码是:
var hc = new HttpClient();
var responseHTTP = await hc.GetAsync("https://www.somedomain.com/api/");
Run Code Online (Sandbox Code Playgroud)
但这会产生以下 HTTP 请求标头:
:authority: www.somedomain.com
:method: GET
:path: /api/json?input=test&key=AIzaSyDqWvsxxxxxxxxxxxxxxxxx1R7x2qoSkc&sessiontoken=136db14b-88bd-4730-a0b2-9b6c1861d9c7
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
origin: http://localhost:5000
referer: http://localhost:5000/places
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36
x-client-data: CJS2yQxxxxxxxxxxxxxxxxxxxxxxxxygEI7bXKAQiOusoBCObGygE=
Run Code Online (Sandbox Code Playgroud) 我创建了一个 Asp.Net Core 托管的 Blazor webassembly 3.2.0 Preview 3 应用程序,其中包含应用程序内帐户的身份验证选项。然后我向 ApplicationUser 类添加了一些额外的属性,并将这些更改迁移到数据库。然后我实现了一个自定义声明工厂,如下所示:
public class MyCustomUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public MyCustomUserClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName ?? string.Empty));
.....
return identity;
}
}
Run Code Online (Sandbox Code Playgroud)
并在服务器应用程序中注册声明工厂,如下所示:
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddClaimsPrincipalFactory<MyCustomUserClaimsPrincipalFactory>();
Run Code Online (Sandbox Code Playgroud)
但是,当我在客户端 Web 应用程序组件中列出声明时,我没有看到我在自定义声明工厂中添加的任何其他声明。我用来列出索赔的代码是:
<AuthorizeView>
<Authorized>
<ul>
@foreach (var claim in context.User.Claims)
{
<li><span>@claim.Type</span><span>@claim.Value</span></li>
}
</ul>
</Authorized>
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)
我确认正在调用声明工厂代码。如何在客户端 Web 应用程序中获取其他声明?
编辑:我甚至用ClaimsTransformer(如建议尝试 …
您好,需要有关从 RederFragment 组件到父方法的事件回调的帮助,下面是代码片段
通用列表组件
@typeparam TItem
@foreach (var item in Items)
{
@ChildContent(item)
}
@code {
[Parameter] public List<TItem> Items { get; set; }
[Parameter] public RenderFragment<TItem> ChildContent { get; set; }
void DeleteItem(TItem item)
{
Items.Remove(item);
InvokeAsync(StateHasChanged);
}
}Run Code Online (Sandbox Code Playgroud)
电话组件
<InputText @bind-Value="NewPhone.PhoneNumber"></InputText>
@code {
public Phone NewPhone { get; set; }
[Parameter] public EventCallback<Phone> DeletePhone { get; set; }
public class Phone
{
public int Id { get; set; }
public string PhoneNumber { get; set; }
}
} …Run Code Online (Sandbox Code Playgroud)我有一个带有 blazor 客户端组件(webassembly/WASM)的正常运行的网站。然后我尝试在谷歌上为该网站做广告,但被不需要的软件拒绝了。我查看了我的网站,在 wwwroot 中当然有一个 _bin_framework 目录,其中包含下载到客户端浏览器的 dll。
这可能是问题吗?什么可能是解决方法?或者,是否有人有 blazor WASM 网站的示例,这些网站是谷歌广告的成功目标,可能证明 Blazor DLL 不是问题,而其他可能是问题?
(事实上,任何关于如何处理谷歌拒绝垃圾软件的一般建议都非常感谢!)
我注意到许多开发人员在 Blazor Server App 和 Blazor WebAssembly App 中错误地将 AuthenticationStateProvider 子类化,并且更重要的是出于错误的原因。
如何正确地做,什么时候做?
asp.net-web-api asp.net-core blazor blazor-server-side blazor-client-side
我想在服务器端和客户端之间使用基于角色的授权。
我可以正确登录并UserManager.IsInRoleAsync(user, "admin")在服务器端返回 True 。
但是在客户端既不工作@attribute [Authorize(Roles = "admin")]也不行<AuthorizeView Roles="admin">。User.Identity.IsInRole("admin")在客户端也返回 False。
如何在客户端获取用户的角色?
// Startup.ConfigureServices()
services.AddDefaultIdentity<ApplicationUser>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 2;
options.Password.RequireNonAlphanumeric = false;
options.User.RequireUniqueEmail = true;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
Run Code Online (Sandbox Code Playgroud)
// Startup.Configure()
app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();
Run Code Online (Sandbox Code Playgroud)
// RolesController.Get()
var userid = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
var currentUser = await …Run Code Online (Sandbox Code Playgroud) c# asp.net blazor-server-side blazor-client-side blazor-webassembly
我正在尝试从我的 Blazor 应用程序向我的 ASP.NET Core API 发送 HTTP 请求。我到处都有断点。应用程序在 API 控制器上的操作方法返回后立即给出异常。我对 .NET 总体上很熟悉,但是,我无法破译错误消息。
Blazor http 调用:
var response = await _httpClient.GetStreamAsync($"Customer/GetAllCustomers");
Run Code Online (Sandbox Code Playgroud)
ASP.NET Core API 控制器操作:
[HttpGet]
[Route("GetAllCustomers")]
public async Task<IEnumerable<Customer>> GetAllCustomersAsync()
{
return await _service.GetAllCustomersAsync();
}
Run Code Online (Sandbox Code Playgroud)
错误堆栈:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: TypeError: Failed to fetch
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) <0x301ab40 + 0x00a30> in <filename unknown>:0
at System.Net.Http.WebAssemblyHttpHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x2ff3590 + 0x00174> in <filename unknown>:0
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync (System.Net.Http.HttpRequestMessage …Run Code Online (Sandbox Code Playgroud) 我<RemoteAuthenticatorView Action="@Action" OnLogInSucceeded="OnLogInSucceeded" />在 Blazor 应用程序的身份验证页面中使用,并注意到它在用户登录时多次调用“OnLogInSucceeded”。我希望它只运行一次。当用户登录时,我必须从数据库中获取一些数据,并认为这就是执行此操作的地方。这并不是说会出现错误,只是每次登录时检索数据 3-4 效率很低。
有没有更好的方法/地方在登录时进行 api 调用?
我正在使用 Azure B2C..