Get login prompt on any button if not already authenticated

New Contributor

Hello!

 

My apologies if this is not the correct space for this topic.

 

I have a requirement where there is about eight (8) bootstrap buttons on a webpage, they each then open a different bootstrap modal that have different functions. I need to prompt the user for their username and password (if not already authenticated)  and then continue on to the desired modal function.  

 

I am using VS 2022. I am hoping to stay with Bootstrap 5.1, JavaScript and using the built in AddAuthentication, ExpireTimeSpan,app.UseAuthentication(), and app.UseAuthorization(); configuration.

 

This to me seems non-standard and would like to stay with this because of how the web page is used.

 

If there is any code or sites I can go and look at to resolve my issue, that would be wonderful. Or, if there is any other thought on how to get to my end results.

 

Thank-you,

Charles

 

 

1 Reply

@CharlesS560 

 

Hi Charles,

 

If you keep in mind that events fired on client side would not go on server side by default, you realise you will need to have a little bit of code on client side for this scenario to work. My suggestion is to pass the authenticated user state in an object to the client side and you can have a global selector for all your modal buttons which checks that state before allowing the modal to open:

 

@section scripts{
    <script>
        $(document).ready(function () {
            var isAuthenticated =  @Html.Raw(user.Identity.IsAuthenticated);
        
    });
    </script>
}