我们有现有的ASP.NET核心应用程序(.NET 5),它使用角作为UI框架。
我们创建了一个Blazor客户端库,我们希望在这个应用程序中使用它,同时使用已经存在的角框架。
在文档之后,我们在Startup.Configure
方法中将其配置为在www.root中的专用目录“balzor app”中“服务”balzor应用程序。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseBlazorFrameworkFiles("/blazor-app");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapFallbackToFile("blazor-app/index.html");
});
}
我们如何配置Blazor应用程序,所以它的index.html只返回给经过身份验证的用户?
比如这样的东西?
[Authorize]
public class ClientController
{
public IActionResult ClientApp()
{
// returns Blazor app index.html
}
}
发布于 2022-01-12 04:29:57
在为视图提供服务之前,有几种方法可以进行身份验证。我决定由ASP.Net会话管理层来做这件事。
将会话服务添加到应用程序中:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(3600); // an hour
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
然后在应用程序中使用会话:
app.UseRouting();
// Call UseSession after UseRouting and before MapRazorPages and MapDefaultControllerRoute
app.UseSession();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
现在,在提供视图之前,检查用户是否经过身份验证:
public IActionResult Index()
{
string KEY = "Authentication-Key";
if (string.IsNullOrEmpty(HttpContext.Session.GetString(KEY))||
!IsValid(HttpContext.Session.GetString(KEY))){
// Redirect to {Action = Index} of {Controller = Login}
return RedirectToAction("Index", "Login");
}
return View(); // consider that this is the blazor view
}
private bool IsValid(string KEY)
{
// Implement your validation mechanism
return KEY == "THIS IS THE AUTH KEY";
}
让我们假设用户被重定向到Login页面之后,该页面被检测到未经过身份验证,并且再次得到身份验证,并接收到一个用于身份验证的密钥:
string KEY = "Authentication-Key";
HttpContext.Session.SetString(KEY, "THIS IS THE AUTH KEY");
最后,当她/他手动或自动返回索引页面(ex:blazor
视图)时,视图将被提供。
发布于 2022-01-14 09:21:10
Index.html是应用程序的入口点。如果您想为授权用户和未经授权的用户提供不同的UI (如果没有菜单),您应该提供不同的布局。下面的代码是App.razor的一部分。关键是要有一个附加的布局组件(UnauthorizedLayout),如果用户没有被授权,就会显示它。此布局中不包含任何元素,其唯一目的是承载登录视图。
AuthenticationService只是一个如何获得对用户的引用的例子,您可以使用您喜欢的任何方法。
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
@if (AuthenticationService.User == null)
{
<AppRouteView RouteData="@routeData" DefaultLayout="@typeof(UnauthorizedLayout)" />
}
else
{
<AppRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
}
@*<FocusOnNavigate RouteData="@routeData" Selector="h1" />*@
</Found>
<NotFound>
...
</NotFound>
UnathorizedLayout.razor:
@inherits LayoutComponentBase
<div id="container">
@Body
</div>
发布于 2022-07-25 04:46:38
<AuthorizeView>
<Authorized>
<main class="main" >
<div class="container-fluid" data-layout="container">
<nav class="navbar navbar-light navbar-vertical navbar-expand-xl">
<NavMenu></NavMenu>
</nav>
<div class="content">
<!-- navbar -->
<Toolbar></Toolbar>
<!-- content -->
@Body
<!-- footer -->
<Footer></Footer>
</div>
</div>
</main>
</Authorized>
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
RedirectToLogin
@inject NavigationManager Navigation
@code {
[CascadingParameter]
private Task<AuthenticationState> AuthenticationStateTask { get; set; }
protected override async Task OnInitializedAsync()
{
var authenticationState = await AuthenticationStateTask;
if (authenticationState?.User?.Identity is null || !authenticationState.User.Identity.IsAuthenticated)
{
Navigation.NavigateTo("/Login", true);
}
}
}
https://stackoverflow.com/questions/70592930
复制