Base solution for your next web application
Open Closed

[error] Authentication action in WebApi AccountController #1053


User avatar
0
feliw created

Hi,

I'm not sure why I'm not able to add another post action in WebApi AccountController. I got this error when I try to access the action

Multiple actions were found that match the request: Authenticate on type Nascence.BodynitsPortal.WebApi.Controllers.AccountController AuthenticateCardNo on type Nascence.BodynitsPortal.WebApi.Controllers.AccountController

Below is my action, one fore normal login, and another with card. Any advice on which part that I do wrong ? Thankss Before. :D

[HttpPost]
        public async Task<AjaxResponse> Authenticate(LoginModel loginModel)
        {
            CheckModelState();

            var loginResult = await GetLoginResultAsync(
                loginModel.UsernameOrEmailAddress,
                loginModel.Password,
                loginModel.TenancyName
                );

            var ticket = new AuthenticationTicket(loginResult.Identity, new AuthenticationProperties());

            var currentUtc = new SystemClock().UtcNow;
            ticket.Properties.IssuedUtc = currentUtc;
            ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));

            return new AjaxResponse(OAuthBearerOptions.AccessTokenFormat.Protect(ticket));
        }
[HttpPost]
        public async Task<AjaxResponse> AuthenticateCardNo(CardLoginModel loginModel)
        {
            CheckModelState();

            var loginResult = await GetCardLoginResultAsync(
                loginModel.Facility,
                loginModel.CardNo
                );

            var ticket = new AuthenticationTicket(loginResult.Identity, new AuthenticationProperties());

            var currentUtc = new SystemClock().UtcNow;
            ticket.Properties.IssuedUtc = currentUtc;
            ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));

            return new AjaxResponse(OAuthBearerOptions.AccessTokenFormat.Protect(ticket));
        }

2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    For current web api routing configuration, more than one post/get action is not allowed in a controller. You can create another controller or change the web api route config.

    Current web api route config does not contain action parameter, so change it like this.

    routes.MapHttpRoute(
    	name: "DefaultApi",
    	routeTemplate: "api/{controller}/{action}/{id}",
    	defaults: new { id = RouteParameter.Optional }
    );
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I forgot the mention, don't change the default mapping for webapi with name "DefaultApi". Instead, add a second mapping and give it an another name.

    This routing configuration might break calls to Zero dynamic web api actions. Please test it and use it carefully :).