English 中文(简体)
Blazor服务器与Microsoft Entra ID认证相配
原标题:Blazor Server app with Microsoft Entra ID authentication

I have a Blazor Server app registered in Microsoft Entra ID and I want to get the logged in user data and some other users data, too. I registered this app in this way:

How can I get these user information (name, uid, email address, etc.) from Azure, please? This solution works for me, but I would like to use it as a service per request in Program.cs:

[CascadingParameter]
private Task<AuthenticationState>? authenticationState { get; set; }

protected override async Task OnInitializedAsync()
{
    if (authenticationState is not null)
    {
        var authState = await authenticationState;
        var user = authState?.User;

        if (user?.Identity is not null && user.Identity.IsAuthenticated)
        {
            authMessage = $"{user.Identity.Name} is authenticated.";
            var claims = user.Claims;

            //These are just examples
            var uid = claims.Where(x => x.Type.Equals("uid")).FirstOrDefault().Value;
            var name = claims.Where(x => x.Type.Equals("name")).FirstOrDefault().Value;
        }
    }
}

UPDATE I tried this but it gives me an error:

无效行动 例外: Cannot提供BlazorServerAppAzureAD.Pages类财产使用者的价值。 指数。 没有关于BlazorServerAppAzureAD的注册服务。 数据。

builder.Services.AddScoped<IUser, User>(st =>
{
    var authState = st.GetService<AuthenticationState>();
    var user = authState?.User;
    if (user?.Identity is not null && user.Identity.IsAuthenticated)
    {
        var claims = user.Claims;

        var uid = claims.Where(x => x.Type.Equals("uid")).FirstOrDefault().Value;
        var name = claims.Where(x => x.Type.Equals("name")).FirstOrDefault().Value;
        var email = claims.Where(x => x.Type.Equals("preferred_username")).FirstOrDefault().Value; 
        return new User { Id = new Guid(uid), Name = name, UserName = user.Identity.Name, Email = email };
    }
    return null;
});
问题回答

你们应当使用中间电线,使用户能够像以下这样的信息。

<>光>

<代码>builder.services.xxx用于登记服务。 <代码>var app = 建造商。 建造;为中值,每个吉大港定居地的申请都按顺序执行。

app.Use(async (context, next) =>
{
    var user = context.User; 

    if (user.Identity.IsAuthenticated)
    {
        //var uid = user.Claims.FirstOrDefault(c => c.Type.Equals("uid"))?.Value;
        var name = user.Claims.FirstOrDefault(c => c.Type.Equals("name"))?.Value;
        var email = user.Claims.FirstOrDefault(c => c.Type.Equals("preferred_username"))?.Value;
        //context.Response.Headers.Add("X-User-UID", new Microsoft.Extensions.Primitives.StringValues(uid));
    }

    await next();
});

“entergraph





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签