locked
System.AggregateException while using SignInAsync. RRS feed

  • Question

  • User-1714333915 posted

    All variables are set an I can't find what I'am missing. But this line await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); keeps throwing an AggregateException.

    Please point me where to look or what I'am missing.

    Here is the full code for better understanding:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Claims;
    using System.Threading.Tasks;
    using asp_xamar_solution.Models;
    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Mvc;

    namespace asp_xamar_solution.CommonFunctions
    {
    public class LoginFunction : Controller
    {
    public bool Login(IUserDataModel userData, LoginModel loginModel)
    {
    if (userData.QUserData.Any(email => email.Email == loginModel.Email && email.Paswword == loginModel.Password))
    {
    Task.Run(async () => await Sign(loginModel)).Wait();
    return true;
    }
    else
    {
    return false;
    }
    }
    private async Task Sign(LoginModel login)
    {
    List<Claim> claims = new List<Claim>()
    {
    new Claim(ClaimTypes.Name, login.Email),
    new Claim(ClaimTypes.Role, "User")
    };
    AuthenticationProperties authProperties = new AuthenticationProperties()
    {
    AllowRefresh = true,
    ExpiresUtc = DateTime.UtcNow.AddMinutes(10),
    IsPersistent = true
    };
    ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);
    }
    }
    }

    Sunday, July 14, 2019 9:28 AM

Answers

  • User665608656 posted

    Hi Harardin,

    You sad I haven't added a "Controller" to a class but it's there public class LoginFunction : Controller

    The Controller in MVC architecture handles any incoming URL request. Controller is a class, derived from the base class System.Web.Mvc.Controller.

    Controller class contains public methods called Action methods. Controller and its action method handles incoming browser requests, retrieves necessary model data and returns appropriate responses.

    This explain comes from: Controller

    ASP.NET MVC uses the controller naming convention to achieve standardization. Unless the compiler has a name + controller structure, the compiler will not recognize the controller class.

    For more details, you could also refer to this link : Why do MVC controllers have to have the trailing 'Controller' convention on their class name?

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 15, 2019 7:23 AM

All replies

  • User475983607 posted

    There's a lot of problems with your code.  The SignInAsync has incorrect arguments, the controller class is missing "Controller".  The async design is totally incorrect.

    I recommend using the standard Identity templates if you are not sure how to build a secured web application.

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio

    Or if you are trying to build cookie auth without Identity.

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-2.2

    Sunday, July 14, 2019 11:55 AM
  • User-1714333915 posted

    Thank you for your answer.

    Hm I tested same in The Controllers folder and all worked. But I am still interested why it didn't worked outside of the Controllers folder inside of a different class.

    You sad I haven't added a "Controller" to a class but it's there public class LoginFunction : Controller

    Can you please give me some advice or a little tip why it give an error?

    My guess for now that IActionResult should be presented, but that's a stupid guess.

    Sunday, July 14, 2019 1:31 PM
  • User665608656 posted

    Hi Harardin,

    You sad I haven't added a "Controller" to a class but it's there public class LoginFunction : Controller

    The Controller in MVC architecture handles any incoming URL request. Controller is a class, derived from the base class System.Web.Mvc.Controller.

    Controller class contains public methods called Action methods. Controller and its action method handles incoming browser requests, retrieves necessary model data and returns appropriate responses.

    This explain comes from: Controller

    ASP.NET MVC uses the controller naming convention to achieve standardization. Unless the compiler has a name + controller structure, the compiler will not recognize the controller class.

    For more details, you could also refer to this link : Why do MVC controllers have to have the trailing 'Controller' convention on their class name?

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 15, 2019 7:23 AM