튜토리얼

.NET Core 개발환경에서 바로써트 SDK를 추가하여 본인인증 요청(requestIdentity) 함수를 구현하는 예시입니다.

1. BaroCert SDK 추가

[프로젝트 > NuGet 패키지 관리] 메뉴에서 barocert 검색하여 최신 버전의 패키지를 설치합니다.

2. BaroCert SDK 설정

프로젝트의 Startup.cs 파일에 네이버써트 서비스 인스턴스 클래스를 생성하고, Startup클래스의 ConfigureServices() 함수에 의존성 주입 패턴으로 Singleton 서비스 인스턴스를 추가합니다.
네이버써트 서비스명으로 컨트롤러를 생성하고 생성한 컨트롤러의 생성자 함수에서 네이버써트 인스턴스 객체를 할당합니다.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Barocert.navercert;
using Microsoft.AspNetCore.Routing;

public class NavercertInstance
{
	// 링크아이디
	private string linkID = "TESTER";
	
	// 비밀키
	private string secretKey = "SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3I=";

	public NavercertService navercertService;

	public NavercertInstance()
	{
		// Navercert 서비스 객체 초기화
		navercertService = new NavercertService(linkID, secretKey);

		// 인증토큰 IP 검증 설정, ture-사용, false-미사용, (기본값:true)
		navercertService.IPRestrictOnOff = true;

		// 통신 고정 IP, true-사용, false-미사용, (기본값:false)
		navercertService.UseStaticIP = false;
	}
}

namespace BarocertExample
{
	public class Startup
	{
		public Startup(IConfiguration configuration)
		{
			Configuration = configuration;
		}

		public IConfiguration Configuration { get; }

		// This method gets called by the runtime. Use this method to add services to the container.
		public void ConfigureServices(IServiceCollection services)
		{
			// Add framework services.
			services.AddMvc();

			services.AddSingleton<NavercertInstance>();
		}

		// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
		public void Configure(IApplicationBuilder app, IHostingEnvironment env)
		{
			if (env.IsDevelopment())
			{
				app.UseDeveloperExceptionPage();
			}
			else
			{
				app.UseExceptionHandler("/Home/Error");
			}

			app.UseStaticFiles();

			app.UseMvc(routes =>
			{
				routes.MapRoute(
					name: "default",
					template: "{controller=Navercert}/{action=Index}");
			});
		}
	}
}

3. RequestIdentity 기능 구현

네이버써트 서비스명으로 생성한 컨트롤러의 생성자 함수에 인스턴스 객체를 할당하고, RequestIdentity 함수 코드를 추가합니다.

/**
* 네이버 이용자에게 본인인증을 요청합니다.
* https://developers.barocert.com/reference/naver/dotnetcore/identity/api#RequestIdentity
*/
public IActionResult RequestIdentity()
{

	// Navercert 이용기관코드, Navercert 파트너 사이트에서 확인
	String clientCode = "023090000021";

	// 본인인증 요청 정보 객체
	Identity identity = new Identity();

	// 수신자 휴대폰번호 - 11자 (하이픈 제외)
	identity.receiverHP = _navercertService.encrypt("01012341234");
	// 수신자 성명 - 80자
	identity.receiverName = _navercertService.encrypt("홍길동");
	// 수신자 생년월일 - 8자 (yyyyMMdd)
	identity.receiverBirthday = _navercertService.encrypt("19700101");

	// 고객센터 연락처 - 최대 12자
	identity.callCenterNum = "1600-9854";
	// 인증요청 만료시간 - 최대 1,000(초)까지 입력 가능
	identity.expireIn = 1000;

	// AppToApp 인증요청 여부
	// true - AppToApp 인증방식, false - 푸시(Push) 인증방식
	identity.appUseYN = false;
	// ApptoApp 인증방식에서 사용
	// 모바일장비 유형('ANDROID', 'IOS'), 대문자 입력(대소문자 구분)
	// identity.deviceOSType = "IOS";
	// AppToApp 방식 이용시, 에러시 호출할 URL
	// "http", "https"등의 웹프로토콜 사용 불가
	// identity.returnURL = "navercert://sign";

	try
	{
		var result = _navercertService.requestIdentity(clientCode, identity);
		return View("requestIdentity", result);
	}
	catch (BarocertException ne)
	{
		return View("exception", ne);
	}
}

4. 결과 확인

함수 호출이 정상적으로 처리되면 "푸시(Push) 인증" 방식은 접수아이디(32자리 숫자)가 반환되며, "앱투앱 인증" 방식은 접수아이디와 AppScheme 이 함께 반환됩니다. 실패인 경우 BarocertException으로 오류코드("-"로 시작하는 8자리 숫자값)와 오류메시지가 반환됩니다. [오류코드]