Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teams Toolkit support for Blazor WASM #13180

Open
dscheiermann opened this issue Feb 12, 2025 · 2 comments
Open

Teams Toolkit support for Blazor WASM #13180

dscheiermann opened this issue Feb 12, 2025 · 2 comments
Assignees
Labels
feature-request The issue is a feature request needs attention This issue needs the attention of a contributor.

Comments

@dscheiermann
Copy link

dscheiermann commented Feb 12, 2025

Is your feature request related to a problem? Please describe.

I have an already existing Blazor WASM application set up with MSAL.
It uses authorized routes and includes role management based on my azure app registration.
Now it needs to be embedded as a teams tab.
The problem is, MSAL authentication with popup flow or redirect do not work in the teams desktop version.
Using the Teams SSO token, I get an authenticated user, but my AuthenticationStateProvider does not recognize the user as authenticated. Which means, that every user is "anonymous". I have already tried using the microsoftTeams.authentication.authenticate to open a popup which loads the authentication page used for MSAL. But it only authenticates the user in the popup and when it closes, the authentication state is lost.

@page "/authentication/{Action}"
@Inject IJSRuntime JS
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication

@code {
[Parameter]
public string Action { get; set; }
}

Describe the solution you'd like

I need some sort of way or idea to combine MSAL and Teams SSO to work with authenticated routes and role management
Maybe MSAL is the wrong approach for this kind of solution?

Describe alternatives you've considered

I have switched to Blazor Server and used an internal API for Cookie authentication to make it work. I dont really like this approach, since im moving from webassembly to server.

Additional context
Add any other context or screenshots about the feature request here.

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs attention This issue needs the attention of a contributor. label Feb 12, 2025
Copy link

👋 Hi @dscheiermann,

Thank you for reporting this issue! We want to let you know that:

⏱️ Our team will personally review your issue
🎯 You can expect a response within 2 business days

✨ This is an AI-generated initial response
🤖 AI-Generated Preliminary Analysis:

It sounds like you're experiencing issues with integrating MSAL authentication in a Blazor WebAssembly (WASM) application within Microsoft Teams, specifically when embedding it as a Teams tab. The main problem is the loss of authentication state when using MSAL with popups or redirects within the Teams desktop version.

Here's a detailed response addressing your problem and suggesting solutions:

Problem Summary:

  1. MSAL Authentication Issues in Teams Desktop:

    • MSAL popup flow does not retain the authentication state when the popup closes.
    • Redirect flow is not viable due to iframe constraints within Teams.
  2. Authentication State in Blazor WASM:

    • Using Teams SSO token provides an authenticated user, but the AuthenticationStateProvider does not recognize the user as authenticated, resulting in every user being treated as "anonymous".

Proposed Solution:

To resolve these issues, you need to combine MSAL and Teams SSO in a way that the authentication state is maintained across your Blazor WASM application. Here are some steps and suggestions:

  1. Utilize Teams SSO for Initial Authentication:

    • Use the Teams Toolkit to obtain the Teams SSO token when the user accesses the tab. This token can be utilized to authenticate the user initially.
  2. Custom AuthenticationStateProvider:

    • Implement a custom AuthenticationStateProvider that uses the Teams SSO token to set the authentication state. This way, the authentication state will be recognized throughout your Blazor WASM application.
  3. MSAL Integration with Teams SSO:

    • Integrate MSAL in a way that it works alongside Teams SSO. Instead of relying solely on MSAL popups or redirects, leverage the Teams authentication mechanisms available.

Example Implementation:

Here's an example of how you can create a custom AuthenticationStateProvider to integrate Teams SSO with Blazor WASM:

using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.JSInterop;
using System.Security.Claims;
using System.Threading.Tasks;

public class TeamsAuthenticationStateProvider : AuthenticationStateProvider
{
    private readonly IJSRuntime _jsRuntime;

    public TeamsAuthenticationStateProvider(IJSRuntime jsRuntime)
    {
        _jsRuntime = jsRuntime;
    }

    public override async Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        var user = await GetUserFromTeamsSSO();
        var identity = user != null
            ? new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, user) }, "TeamsSSO")
            : new ClaimsIdentity();

        var userClaimsPrincipal = new ClaimsPrincipal(identity);
        return new AuthenticationState(userClaimsPrincipal);
    }

    private async Task<string> GetUserFromTeamsSSO()
    {
        // Use JSInterop to call Teams SDK and get the user information
        var user = await _jsRuntime.InvokeAsync<string>("microsoftTeams.authentication.getAuthToken");
        return user;
    }
}

Additional Resources:

Alternative Considerations:

  • If integrating Teams SSO and MSAL in Blazor WASM proves too complex, consider using Blazor Server with cookie-based authentication as a fallback method. This approach, although not preferred, might provide a more stable authentication flow within Teams.

By following these steps and implementing a custom AuthenticationStateProvider, you should be able to maintain the authentication state across your Blazor WASM application when embedded as a Teams tab. This approach combines the strengths of Teams SSO with the flexibility of Blazor's authentication mechanisms. If you encounter further issues, feel free to consult the links provided or reach out to the Teams Toolkit community for more assistance.

For more detailed guidance, you can refer to the GitHub issue related to similar integration challenges.


Note: This is an automated first response generated by AI. A human team member will review your issue and provide a more detailed response soon. We appreciate your patience!

@Nivedipa-MSFT
Copy link

Thank you for bringing this issue to our attention. We will look into it and get back to you shortly.

@MSFT-yiz MSFT-yiz added the feature-request The issue is a feature request label Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request The issue is a feature request needs attention This issue needs the attention of a contributor.
Projects
None yet
Development

No branches or pull requests

4 participants