I am writing an internal application using Blazor Server. So far, all I have done is create a new project using authentication and connected it a registered app in Azure that only allows users on our domain to to access. This works out of the box, when I start the application I am immediately shown the Microsoft sign in page before I can access the application. However, is there a way to allow users to hit a landing page first before choosing to login? I cant see to find a way to do this.
Thanks.
CodePudding user response:
Probably you have a service like that being added:
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});
This will automatically challange the user to login if he tries to access any page of your application, so I recommend removing it.
Next, you need a login and logout button. Create it somewhere in the MainLayout.razor
:
<AuthorizeView>
<Authorized>
Hello, @context.User.Identity?.Name!
<a href="MicrosoftIdentity/Account/SignOut">Log out</a>
</Authorized>
<NotAuthorized>
<a href="MicrosoftIdentity/Account/SignIn">Log in</a>
</NotAuthorized>
</AuthorizeView>
Since we removed the Authorization filter from your pages, you might want to decorate your secured pages manually using this code in top of the blazor components:
@attribute [Authorize]