English 中文(简体)
。 NET MAUI B. 拥有共享的国库图书馆和认证的布莱克
原标题:.NET MAUI Blazor with a shared UI library and authentication

页: 1 NET MAUI Blazor Application, that share the UI with a traditional Blazor application, as can see in the Blazor Weather example found at https://github.com/danroth27/BlazorWeather

我对这一公式的歪曲是,我希望能够在我的申请中使用认证,并分享尽可能多的法典。 理想的情况是,我想把所有重复逻辑放在共同的“倡议”中(以及当然还有网上和MAUI的版本)。

I ve tried to create a very basic sample Solution so you could see what my issue is. The public repository should be accessible at https://github.com/Dukenware/OneToRuleThemAll

下面的步骤:

  1. I ve started by creating a Blazor Server with default authentication of type "Individual Accounts".
  2. Tested that the application runs as expected.
  3. Created a Razor Class Library project that I ve added to the solution.
  4. Moved all logic from BlazorServer to the RazorClassLibrary project.
  5. Fixed HTML styling and so on so the application runs as before.
  6. Added MAUI Blazor project.
  7. Tested that the MAUI application runs as expected.
  8. Referenced the RazorClassLibraryProject.
  9. Tried displaying the UI from RazorClassLibrary by running the RazorClassLibrary.App from MauiBlazor s Main.razor.
  10. Getting the authentication exception:
GetAuthenticationStateAsync was called before SetAuthenticationState.
   at Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider.GetAuthenticationStateAsync()
   at Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState.OnInitialized()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

如果有人能向我指出我可以用来在我申请的基础上建造工作的寺庙,我真心 app。 任何反馈都受到热烈欢迎。

问题回答

我也谈到这一问题。

这就是我发现的。 当我的美国国没有恢复正确结果时,我正在发现这一错误。 它是执行违约判决国提供方的习俗证明:

public class CustomAuthenticationStateProvider :AuthenticationStateProvider
{

    public async Task Login(string token)
    {
        await SecureStorage.SetAsync("accounttoken", token);
        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
    }

    public async Task Logout()
    {
        SecureStorage.Remove("accounttoken");
        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
    }

    public override async Task<AuthenticationState>GetAuthenticationStateAsync()
    {

        try
        {
            var userInfo = await SecureStorage.GetAsync("accounttoken");
            if (userInfo != null)
            {
                var claims = new[] { new Claim(ClaimTypes.Name, "User") };
                var identity = new ClaimsIdentity(claims, "Server authentication");
                return new AuthenticationState(new ClaimsPrincipal(identity));
            }
        }
        catch (HttpRequestException ex)
        {
            //This should be implemented better
            Console.WriteLine("Request failed:" + ex.ToString());
        }

        return new AuthenticationState(new ClaimsPrincipal());
     }
}

然后,我马伊·普拉塔姆 我补充说:

builder.Services.AddAuthorizationCore();
builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>();

我的回来不是完全的。

new AuthenticationState(new ClaimsPrincipal());

造成同样的错误:

GetAuthenticationStateAsync was called before SetAuthenticationState.
at Microsoft.AspNetCore.Components.Server.ServerAuthenticationStateProvider.GetAuthenticationStateAsync()
at Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState.OnInitialized()
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

希望这一帮助!

How can I Add page to BlazorServer and navigate to that page ? https://github.com/Dukenware/OneToRuleThemAll/issues/1





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签