【问题标题】:Blazor server: read username outside of componentsBlazor 服务器:在组件之外读取用户名
【发布时间】:2021-04-14 21:59:38
【问题描述】:

我正在尝试开始使用 Blazor Server。请多多包涵 - 这是我迈向网络的第一步。

我的小应用程序只包含一个页面,其中包含一些保存在数据库中的输入字段。为了识别用户,我在 .razor 页面中添加了以下代码:(在代码隐藏中)

public partial class MyPage
    {
 [Inject] AuthenticationStateProvider AuthenticationStateProvider { get; set; } 
 protected override async Task OnInitializedAsync()
        {
            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            var user = authState.User;
            if (user.Identity.IsAuthenticated)
            {
                sessionController.UserKid = user.Identity.Name;
            }
            else
            {
                //userAuthenticated = "The user is NOT authenticated.";
            }
        }
…

现在我想添加第二页(以及以后更多)。但是由于我没有一个通用的“登陆页面”,所以我必须在每个页面中重新插入上面的代码! 所以我想在显示页面之前启动应用程序时对用户进行身份验证。在剃须刀组件之外。但现在我不知道在哪里做得最好。这是否属于“startup.cs”?它是如何工作的?

我刚刚尝试了以下方法:我通过以下方式使我的课程“SessionController”可用:

startup.cs:
  public void ConfigureServices(IServiceCollection services)
    {
    …
    services.AddScoped<SessionController>();
    }  

授权被确定并保存在“SessionController”类中:我在构造函数中调用用户认证。

SessionController:
public class SessionController
    {
        private string _UserKid = "";
        public string UserKid 
        {
            get {
                return _UserKid;
            }
            set 
            {
    --> … Fetching User from Database via "value" as key …
        _UserKid = value;
            }
        }

        
        [Inject] AuthenticationStateProvider AuthenticationStateProvider { get; set; } 
        protected async Task Authorisation()
        {
            //Why ist it always null ?
            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            var user = authState.User;
            if (user.Identity.IsAuthenticated)
            {
                this.UserKid = user.Identity.Name;
            }
            else
            {
                //userAuthenticated = "The user is NOT authenticated.";
            }
        }

        public SessionController()
        {
            Authorisation().Wait();
        }
} 

我的问题:

var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync ();

AuthenticationStateProvider 在这里始终为 NULL。 我不太明白为什么。你能帮帮我吗?

非常感谢!

【问题讨论】:

标签: c# blazor


【解决方案1】:

我会自己回答我的问题,因为我想得太复杂了!解决方法很简单:

我不必单独对每个页面进行身份验证,只要在“MainLayout.razor”中进行就足够了!该组件被所有“页面”使用。

为了让其他页面可以使用用户数据,我创建了一个“Scoped”类:

public class SessionController
{
    public void InitUser(ClaimsPrincipal User)
    {
      ....doing some Authentication-stuff....

如果我将以下代码添加到“startup.cs”,所有页面都可以使用:

所以它对我有用。

public void ConfigureServices (IServiceCollection services)
         {
             ...
             services.AddScoped <SessionController> (); /

         }

我必须在每个页面中

[Inject] SessionController sessionController { get; set; } 

所以我可以这样使用它:

@if(sessionController.noSuchUser == true)
{
     <div class="row justify-content-center align-items-center alert alert-danger" role="alert">
         Sie haben keine Berechtigung für das Programm!
     </div>
}
else 
{   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2020-11-07
    • 2020-05-06
    相关资源
    最近更新 更多