이것은 Asp.Net WEB API 프로젝트입니다. 나는 사용자가 사이트에 등록하게하고 액세스 권한을 부여하기 전에 이메일을 확인하기 위해 사용자에게 전자 메일을 보냅니다. 이메일 확인을위한 토큰을 등록하고 생성 할 수 있지만 콜백 URL 생성이 실패합니다. 지금까지 제 코드가 있습니다.ASP.NET 웹 API 2 - 사용자 등록 전자 메일 확인 실패
AccountController.cs
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(UserModel userModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
IdentityResult result = await authRepository.RegisterUser(userModel);
IHttpActionResult errorResult = GetErrorResult(result);
if (errorResult != null)
{
return errorResult;
}
return Ok();
}
AuthRepository.cs
public async Task<IdentityResult> RegisterUser(UserModel userModel)
{
IdentityUser user = new IdentityUser
{
UserName = userModel.UserName,
Email = userModel.UserName
};
IdentityResult result = await userManager.CreateAsync(user, userModel.Password); //var result
if (result.Succeeded)
{
try
{
var provider = new DpapiDataProtectionProvider("TestWebAPI");
userManager.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser>(provider.Create("EmailConfirmation"));
var code = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);
UrlHelper urlHelper = new UrlHelper();
var callbackUrl = urlHelper.Action
(
"ConfirmEmail", "Account",
new { userId = user.Id, code = code },
protocol: "http"
);
await userManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm...." + "<a href=\"" + callbackUrl + "\">click here</a>");
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
return result;
}
그것은 내가 callbackurl를 생성하는 시점에서 실패합니다. 나는이 예외가 ". 값 null 일 수 없습니다 \ 연구 \ 이름 nParameter : routeCollection" 는
스택 추적
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues, String protocol)
at TestWebAPI.AuthRepository.<RegisterUser>d__1.MoveNext() in d:\Workspace\VKalpa 2.0\Projects\Test\WebAPI-0.2\TestWebAPI\TestWebAPI\AuthRepository.cs:line 44
누군가가 도와 주실 수 있습니까?
시도해 보았습니다! 나는 System.Web.Mvc.UrlHelper를 사용했다. –
'새로운 예외 (ex.ToString())를 던지기 '를 사용하는 것은 간단합니다. 단지 예외를 잡는 누군가의 디버깅 경험을 망치지 않기 위해 간단한'throw '를 사용하십시오. – ProfK
@ProfK, 코드가 업데이트되었습니다. –