title: How to create ASP.NET Core Web API properly
body: <p>I little unsure of my actions. I started my first web api application. So I wanna create api where is all functionality include authorization. And web and mobile applications which will work with my api. Something like that<a href="https://i.stack.imgur.com/30NOH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/30NOH.png" alt=" " /></a></p>
  <p>But I discover problem on authorization step. I must handle <strong>many</strong> api's response variation like 401(unauthorized),200(OK) etc. I get response in Json format. And every response have own structure and its structure changes in differet cases. So that is the problem I can miss handle something and crash my app. How can I avoid it. May be I missunderstand something. I will be greateful for any help.</p>
  <p>I create API on asp.net core framework.</p>
  <p>Some response examples</p>
  <p><strong>OK 200</strong>
  <code>{&quot;result&quot;:{&quot;succeeded&quot;:true,&quot;isLockedOut&quot;:false,&quot;isNotAllowed&quot;:false,&quot;requiresTwoFactor&quot;:false},&quot;token&quot;:&quot;eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiI5YjkwZDFmZC1iMjQzLTRhMTEtYWQ3NS1iZWU0ZDJjNTJhNTEiLCJ1bmlxdWVfbmFtZSI6IkVlZm9zZmF0dXMxMzNAZ21haWwuY29tIiwibmJmIjoxNTkzOTU4MjM4LCJleHAiOjE1OTQwNDQ2MzgsImlhdCI6MTU5Mzk1ODIzOH0.AUjS7ocjp3Z_HuU1QqBPUG4NlNcRAihjOhbKBAC_6ecjjlZQM417M9KKGEk1uAr0yKFl9dcPNo04YPSKs-vJ7g&quot;}</code></p>
  <p><strong>401 Unauthorized</strong></p>
  <pre><code>{&quot;type&quot;:&quot;https://tools.ietf.org/html/rfc7235#section-3.1&quot;,&quot;title&quot;:&quot;Unauthorized&quot;,&quot;status&quot;:401,&quot;traceId&quot;:&quot;|9ca7ae31-444c9220bfc1657b.1.88f5b6d2_&quot;}
  </code></pre>
  <p>API's action for example</p>
  <pre><code>public async Task&lt;IActionResult&gt; Login(LoginModel loginModel)
      {
          if (ModelState.IsValid)
          {
              Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.PasswordSignInAsync
                  (loginModel.Email, loginModel.Password, loginModel.RememberMe, false);
  
              if (result.Succeeded)
              {
                  User user = await _userManager.FindByNameAsync(loginModel.Email);
                  ServiceResponse response = new ServiceResponse()
                  {
                      Result = result,
                      Token = CreateToken(user)
                  };
                  return Ok(response);
              }
              else
              {
                  return Unauthorized();
              }
          }
          return NoContent();
  
      }
  </code></pre>