问题
Looking forward to adding authentication to the MVC 5 Boilerplate template, The next piece of code worked well in its own original sample project, but when integrated its content into the Boilerplate template, and tried to register a new user, something become conflicting and a browser exception appears, pointing to the following "await" line:
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");
ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
+ "before you can log in.";
ViewBag.Link = callbackUrl;
return View("Info");
}
AddErrors(result);
}
return View(model);
}
I've read in many places that when an async issue happens, people advise to make it synchronous, but for my case many things would become incompatible.I wonder how to keep this method async as it was originaly in the template,
回答1:
Your GetItems
doesn't need to await
, so it shouldn't be async
. Just change the signature to:
private List<SyndicationItem> GetItems(CancellationToken cancellationToken)
and change the calling code from:
await GetItems(token);
to:
GetItems(token);
回答2:
Thanks to Nikita1315 help, I could find that the next configuration syntax was missing in the startup.cs file:
ConfigureAuth(app);
so the startup class would look like:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureContainer(app);
ConfigureAuth(app);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}
}
So, as soon as I added this configuration, and without modifying any async method, I could register my first user.
来源:https://stackoverflow.com/questions/33484905/trying-to-add-authentication-to-asp-net-mvc-boilerplate-template-async-await