Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:44:18+00:00 2026-06-05T16:44:18+00:00

I am looking to authenticate a user from a client application while using the

  • 0

I am looking to authenticate a user from a client application while using the ASP.NET Web API. I have watched all the videos on the site and also read this forum post.

Putting the [Authorize] attribute correctly returns a 401 Unauthorized status. However, I need to know how to allow a user to log in to the API.

I want to provide user credentials from an Android application to the API, get the user logged in, and then have all subsequent API calls pre-authenticated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:44:19+00:00Added an answer on June 5, 2026 at 4:44 pm

    allow a user to log in to the API

    You need to send a valid Forms Authentication cookie along with the request. This cookie is usually sent by the server when authenticating (LogOn action) by calling the [FormsAuthentication.SetAuthCookie method (see MSDN).

    So the client needs to perform 2 steps:

    1. Send an HTTP request to a LogOn action by sending the username and password. In turns this action will call the FormsAuthentication.SetAuthCookie method (in case the credentials are valid) which in turn will set the forms authentication cookie in the response.
    2. Send an HTTP request to an [Authorize] protected action by sending along the forms authentication cookie it retrieved in the first request.

    Let’s take an example. Suppose that you have 2 API controllers defined in your web application:

    The first one responsible for handling authentication:

    public class AccountController : ApiController
    {
        public bool Post(LogOnModel model)
        {
            if (model.Username == "john" && model.Password == "secret")
            {
                FormsAuthentication.SetAuthCookie(model.Username, false);
                return true;
            }
    
            return false;
        }
    }
    

    and the second one containing protected actions that only authorized users can see:

    [Authorize]
    public class UsersController : ApiController
    {
        public string Get()
        {
            return "This is a top secret material that only authorized users can see";
        }
    }
    

    Now we could write a client application consuming this API. Here’s a trivial console application example (make sure you have installed the Microsoft.AspNet.WebApi.Client and Microsoft.Net.Http NuGet packages):

    using System;
    using System.Net.Http;
    using System.Threading;
    
    class Program
    {
        static void Main()
        {
            using (var httpClient = new HttpClient())
            {
                var response = httpClient.PostAsJsonAsync(
                    "http://localhost:26845/api/account", 
                    new { username = "john", password = "secret" }, 
                    CancellationToken.None
                ).Result;
                response.EnsureSuccessStatusCode();
    
                bool success = response.Content.ReadAsAsync<bool>().Result;
                if (success)
                {
                    var secret = httpClient.GetStringAsync("http://localhost:26845/api/users");
                    Console.WriteLine(secret.Result);
                }
                else
                {
                    Console.WriteLine("Sorry you provided wrong credentials");
                }
            }
        }
    }
    

    And here’s how the 2 HTTP requests look on the wire:

    Authentication request:

    POST /api/account HTTP/1.1
    Content-Type: application/json; charset=utf-8
    Host: localhost:26845
    Content-Length: 39
    Connection: Keep-Alive
    
    {"username":"john","password":"secret"}
    

    Authentication response:

    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/10.0.0.0
    Date: Wed, 13 Jun 2012 13:24:41 GMT
    X-AspNet-Version: 4.0.30319
    Set-Cookie: .ASPXAUTH=REMOVED FOR BREVITY; path=/; HttpOnly
    Cache-Control: no-cache
    Pragma: no-cache
    Expires: -1
    Content-Type: application/json; charset=utf-8
    Content-Length: 4
    Connection: Close
    
    true
    

    Request for protected data:

    GET /api/users HTTP/1.1
    Host: localhost:26845
    Cookie: .ASPXAUTH=REMOVED FOR BREVITY
    

    Response for protected data:

    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/10.0.0.0
    Date: Wed, 13 Jun 2012 13:24:41 GMT
    X-AspNet-Version: 4.0.30319
    Cache-Control: no-cache
    Pragma: no-cache
    Expires: -1
    Content-Type: application/json; charset=utf-8
    Content-Length: 66
    Connection: Close
    
    "This is a top secret material that only authorized users can see"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to oAuth and looking to build a web application using Twitter
A bit of background may help. I'm looking to authenticate a user using SAML,
I have been looking for information on how to authenticate to the facebook api
In a web application already using django.contrib.auth for authentication, I'm looking for the standard
I have several Silverlight, WP7 and ASP.NET MVC client applications Most allow anonymous access
How can I implement following in ASP.NET MVC application: user opens intranet website user
I'm building a web application using Stripes and Spring. It needs to have a
Looking to do a bit of refactoring... Using NHibernate I have this query currently
We are attempting to integrate an ASP.NET MVC site with our client's SSO system
I have two ASP.NET pages: site.com/foo/bar.aspx that should be world accessible and site.com/foo/baz.aspx that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.