[Blazor] getting current user details in a Blazor component[*.cs] > C#/.Net/Blazor/IIS

본문 바로가기
사이트 내 전체검색

C#/.Net/Blazor/IIS

[Blazor] getting current user details in a Blazor component[*.cs]

페이지 정보

작성자 sbLAB 댓글 0건 조회 9,358회 작성일 21-09-18 00:09

본문

https://docs.microsoft.com/ko-kr/aspnet/core/blazor/security/?view=aspnetcore-5.0 <--  이렇게 해야함.


 using Microsoft.AspNetCore.Components.Authorization;


  public partial class Cyber : ComponentBase

    {

        ...

        //Blazor 로그인 인증정보 접근  Inject

        [Inject] AuthenticationStateProvider AuthenticationStateProvider { get; set; } 

        ...


        protected override async Task OnInitializedAsync()

        {

            cyberReqModel_form.UserId = await GetClaimsPrincipalData_userIdentityName();

        }



        //로그인정보 얻기

        private async Task<string> GetClaimsPrincipalData_userIdentityName()

        {

            var _authMessage="";

            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

            var user = authState.User;


            if (user.Identity.IsAuthenticated) {

                _authMessage = user.Identity.Name;

            }

            else {

                _authMessage = "none";

            }

            return _authMessage;

        }


    }


---------------------------------------------------------------------------



아래소스는 정상작동 안됨(http 연결 끊김[disconnected])


CodeBehind Style

[Startup.cs]

public void ConfigureServices(IServiceCollection services)

{

    ...

    services.AddHttpContextAccessor();  //cs코드에서 Auth ID 사용가능

    ... 

}


[Cyber.cs] 

using Microsoft.AspNetCore.Http;


public partial class Cyber : ComponentBase{

        ...

        [Inject]

       IHttpContextAccessor httpContextAccessor { get; set; } //Blazor 로그인 인증정보 접근  Inject

        ...

       public string AuthUserId;

    }



protected override async Task OnInitializedAsync(){

            AuthUserId = httpContextAccessor.HttpContext.User.Identity.Name;

 

    }


@Code in razor Style 

https://www.syncfusion.com/faq/blazor/javascript-interop/how-do-you-get-current-user-details-in-a-blazor-component 

[이 게시물은 sbLAB님에 의해 2022-12-22 09:42:22 Web/PHP/API에서 이동 됨]

댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
244
어제
396
최대
1,279
전체
222,294

그누보드5
Copyright © sebom.com All rights reserved.