2

I totally understand if someone finds that my question is very basic or might not make a lot of sense all the way. I am new to this and I am trying to use the latest .NET Framework 5 with MVC 6 in order to build a Web Api that could be used from an Angular JS client-side. This will allow me to create a website for it, as well as a mobile application by wrapping it with Phonegap. So please bear with me a bit.

What I am trying to achieve for the moment is to have a Web API controller that receives a login request and returns a result to the client based on Cookie Authentication (later the client should store this cookie and use it for communications with the server)

  • I added the following in the project.json

enter image description here

  • In the Startup.cs, I added under ConfigureServices:

    // Add entity framework support
    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]);
        });
    
        // add ASP.NET Identity
        services.AddIdentity<ApplicationUser, IdentityRole>(options => {
            options.Password.RequireDigit = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireNonLetterOrDigit = false;
            options.Password.RequiredLength = 6;
        })
           .AddEntityFrameworkStores<ApplicationDbContext>()
           .AddDefaultTokenProviders();
    
  • In the Startup.cs, under Configure:

        // Using the identity that technically should be calling the UseCookieAuthentication
        app.UseIdentity();
    

Now, in the Controller method to login, I am able to find the user using its email address and the UserManager:

            // Verify that the model is valid according to the validation rules in the model itself. 
            // If it isn't valid, return a 400 Bad Request with some JSON reviewing the errors
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }

            // Find the user in our database.  If the user does not exist, then return a 400 Bad Request with a general error.
            var user = await userManager.FindByEmailAsync(model.Email);
            if (user == null)
            {
                ModelState.AddModelError("", INVALID_LOGIN_MESSAGE);
                return HttpBadRequest(ModelState);
            }

            // If the user has not confirmed his/her email address, then return a 400 Bad Request with a request to activate the account.
            if (!user.EmailConfirmed)
            {
                ModelState.AddModelError("Email", "Account not activated");
                return HttpBadRequest(ModelState);
            }

            // Authenticate the user with the Sign-In Manager
            var result = await signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
            // If the authentication failed, add the same error that we add when we can't find the user
            // (so you can't tell the difference between a bad username and a bad password) and return a 400 Bad Request
            if (!result.Succeeded)
            {
                ModelState.AddModelError("", INVALID_LOGIN_MESSAGE);
                return new BadRequestObjectResult(ModelState);
            }

            return Ok();

The problem is happening at the line:

            // Authenticate the user with the Sign-In Manager
            var result = await signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);

it is throwing the following error:

Error: No authentication handler is configured to handle the scheme: Microsoft.AspNet.Identity.Application

I am currently blocked and I searched googled for almost every possible token I could think of and tried multiple solution still in no vain. Any help is highly appreciated.

Regards,

1 Answer 1

5

Ok I finally figured it out after writing this whole question and I wanted to share the answer to avoid the hussle for someone else if they commit the same mistake I did!

The problem was that in the Configure in Startup.cs, I called "app.UseIdentity()" after calling "app.UseMVC()". The order should have been inversed. I donno if this is common knowledge or I should have read about it somewhere.

Sign up to request clarification or add additional context in comments.

1 Comment

That's like an unsaid rule that one is "supposed" to know when dealing with an owin like pipeline. Most of these things have been obscured for a long time and now because of the pipeline model they are obvious.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.