在渲染器应用中,如果渲染模式设置为“服务器",是否有可能获得请求的路径? [英] In a blazor app, is it possible to get the requested path, if the render mode is set to "server"?

查看:112
本文介绍了在渲染器应用中,如果渲染模式设置为“服务器",是否有可能获得请求的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Blazor客户端应用程序,并且在该应用程序中,我具有许多具有自定义要求和处理程序的授权策略.其中之一检查URL中请求的ID,并检查登录用户是否可以查看此资源.

I created a Blazor client app and and within this app I have a number of authorization policies with custom requirements and handlers. One of them checks the ID requested in the URL and checks whether the logged in user can view this resource.

例如,通过客户端,用户导航到 https://localhost/resource/1f28e41c-bc75-44d6-9eef-d46b66b649c7 这是我的API上的资源.

For example, through the client, the user navigates to https://localhost/resource/1f28e41c-bc75-44d6-9eef-d46b66b649c7 which is a resource on my API.

我正在使用以下代码查看请求路径:

I’m using the following code to see the request path:

var httpContext = _httpContextAccessor.HttpContext;
string requestedPath = httpContext.Request.Path.ToString();

这曾经有效,requestedPath确实包含值"1f28e41c-bc75-44d6-9eef-d46b66b649c7"

This used to work and requestedPath would indeed contain the value "1f28e41c-bc75-44d6-9eef-d46b66b649c7"

但是,在_Host.cshtml中,我已将渲染模式从"ServerPrerendered"更改为"Server".这是因为在页面调用期间,代码在不同的位置执行了两次.

However, in the _Host.cshtml I have changed the render mode from "ServerPrerendered" to "Server". This was due to the fact that the code was executed twice at different places during page invocation.

由于我更改了此设置,所以所请求的路径值始终为"/_blazor".

And since I changed this, the requestedPath value is always "/_blazor".

所以我想知道,在渲染器应用程序中,如果将渲染模式设置为服务器",是否可以获取请求的路径?

So I was wondering, in a blazor app, is it possible to get the requested path if the render mode is set to "server"?

推荐答案

我创建了Blazor客户端应用

I created a Blazor client app

不,你没有.您的应用程序是Blazor Server应用程序(也称为服务器端Blazor应用程序).

No you didn't. Your application is a Blazor Server App (also known as server-side Blazor app).

由于您的应用程序是基于WebSocket连接的,而不是基于HTTP的,因此您不能也不应该尝试访问HttpContext对象.像您使用的那样,基于SignalR的应用程序中不存在HttpContext.

As your app is WebSocket-connection-based, and not HTTP-based, you cannot and should not try to access the HttpContext object. HttpContext does not exist in SignalR-based application like the one you use (Blazor Server App).

以下代码段创建了一个名为Profile的Razor组件,并带有一个名为ID的参数(路由值),您应该将该参数传递给IAuthorizationHandler

The following code snippet creates a Razor Component named Profile, with a parameter (route value) named ID, which you should pass to your IAuthorizationHandler

@page "/profile"
@page "/profile/{id}"

 <AuthorizeView Policy="Place here the name of your policy" 
                                                Resource="@ID">
      <NotAuthorized>
        <h2 class="mt-5">You are not authorized to view this page</h2>
      </NotAuthorized>
      <Authorized>
        <div class="container my-profile">
            <h2>My Profile</h2>
            --- Place here all the content you want your user to view ----
        </div>
      </Authorized>
</AuthorizeView>

@code {

   [Parameter]
   public string ID { get; set; }

}

要记住的注意事项:配置文件ID通过AuthorizeView.Resource属性传递给您的处理程序方法

Note to remember: The profile ID is passed to your handler method through the AuthorizeView.Resource attribute

在处理程序方法中,您可以执行以下操作:

And in the handler method you can do something like this:

 public Task HandleAsync(AuthorizationHandlerContext context)
    {
        if (context == null) return Task.CompletedTask;

        // get the profile id from resource, passed in from the profile page 
        // component
        var resource = context.Resource?.ToString();
        var hasParsed = int.TryParse(resource, out int profileID);
        if (hasParsed)
        {
            // compare the requested profileID to the user's actual claim of 
            // profileID
            var isAuthorized = profileID == context.User.GetProfileIDClaim();
            -- --- I can't code blindly any more-----
        }

    }

希望这会有所帮助.

这篇关于在渲染器应用中,如果渲染模式设置为“服务器",是否有可能获得请求的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆