title: How to remove duplicate login check code in asp.net web service?
body: <p>I have an asp.net web service page (<em>asmx</em>) containing 10+ web-service methods. None of these methods are using session (will not use in future either). As user is required to be authenticated for every call, each of the methods are taking <em>userName</em>, <em>password</em> as first two parameters. So, first few lines of all these methods look same as this:</p>
  
  <pre><code>public Response MyService(string userName, string password, &lt;... other params&gt;)
  {   
      Identity userIdentity;
  
      using (var credentialManager = new CredentialManager())
      {
          userIdentity = credentialManager.GetLoginIdentity(userName, password);
  
          if (userIdentity==null)
          {
              Log.Info("Login attempt failed for: {0}", userName);
              return Response.ErrorResponse(credentialManager.ErrorMessage);
          }
  
          Log.Info("Logged in: {0}", userName);
      }   
  
      // Actual service code 
      ...
      ...
  
  }
  
  //These are custom: Response, Identity, CredentialManager, Log
  </code></pre>
  
  <p>I don't like duplicating these lines for each new method. Also, whenever any <em>log</em> needs to be reformatted or added, it is a mess.</p>
  
  <p>Any suggestion to remove this duplicate code?</p>
  
  <p>(sorry for not finding an appropriate title for the question)</p>