繁体   English   中英

.Net Core Azure AD单租户认证

[英].Net Core Azure AD single tenant Authentication

我有一个 .net 核心 blazor 服务器应用程序,我正在使用 Azure AD 身份验证实现它。 我注册了应用程序并在 azure 端完成了所有设置。 现在,我想检索用户的名称或 email 并在登录时显示欢迎,“用户”消息。 应用程序成功通过身份验证,但我不确定如何检索用户名。

我的代码:Startup.cs

  services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
.AddOpenIdConnect(options =>
{
    options.Authority = "https://login.microsoftonline.com/;
    options.ClientId = ClientId";
    options.ResponseType = OpenIdConnectResponseType.IdToken;
    options.CallbackPath = "/Myrequest";
    options.SignedOutRedirectUri = "https://fftest.azurewebsites.net/";
    options.TokenValidationParameters.NameClaimType = "name";
})

.AddCookie();

        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        services.AddScoped(sp =>
        {
            var provider = sp.GetService<AuthenticationStateProvider>();
            var state = provider.GetAuthenticationStateAsync().Result;
            return state.User.Identity.IsAuthenticated ?
                state.User : null;
        });

    }

索引.razor:

<h3>Welcome @Name ,</h3>



@code {
    public string Name { get; set; }
    //tring email = .FindFirstValue(ClaimTypes.Email);


    protected override async Task OnInitializedAsync()
    {

        var authstate = await Authentication_.GetAuthenticationStateAsync();
        var user = authstate.User.Identity.Name ;
        if (user != null)
        {
            Name = user.ToString();
        }
        else
        {
            Name = "";
        }

这是我第一次尝试做 azure 广告身份验证,我需要实现 Graph API 吗? 任何答案都非常感谢!

如果您从模板生成 Blazor 服务器端项目并启用身份验证,您可以在 Shared/LoginDisplay.razor 页面中看到我将要分享的内容,但这正是您所要求的。

您会看到它将从注入 AuthorizeView 的 AuthenticationState 中读取用户身份到带有@contexdt.User.Identity.Name的页面

如果您想访问相同的内容,可以在任何页面/组件中使用以下内容来注入 AuthenticationState 并读出属性。

@page "/"
@inject AuthenticationState AuthState

<h1>Hello @AuthState.User.Identity.Name!</h1>

要回答您的最后一个问题,无需使用图表 API,因为默认情况下,名称属性可从登录时传回的声明中获得。如果不是这种情况,您可能非常需要从图表 API 请求它。

在花了几天时间之后; 我发现在设置身份验证方面我做的一切都是正确的。 app.razor 文件中缺少级联身份验证 state。

  <CascadingAuthenticationState>
            <Router AppAssembly="@typeof(Program).Assembly">
                <Found Context="routeData">
                    <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
                </Found>
                <NotFound>
                    <LayoutView Layout="@typeof(MainLayout)">
                        <p>Sorry, there's nothing at this address.</p>
                    </LayoutView>
                </NotFound>
            </Router>
        </CascadingAuthenticationState>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM