Coder Social home page Coder Social logo

localaccountsapp's Introduction

Secure a Web API with Individual Accounts and Local Login

This sample shows how to perform local login in ASP.NET Web API.

If you want to recreate this app from scratch, here are the steps that I followed.

  1. Create a new ASP.NET Web Application project. In the New Project dialog, select the Web API template.

  2. If the dialog does not list Individual User Accounts under Authentication, click Change Authentication. Then select Individual User Accounts.

  3. Add Knockout.js to the project, using NuGet. From the Tools menu, select NuGet Package Manager > Package Manager Console. In the console window, type the following command.

    Install-Package knockoutjs

    This adds the Knockout.js files to your Scripts folder.

  4. In App_Start/BundleConfig.cs, add a new script bundle.

     bundles.Add(new ScriptBundle("~/bundles/app").Include(
               "~/Scripts/knockout-{version}.js",
               "~/Scripts/app.js"));
    
  5. Add the app.js file to Scripts folder. The code in app.js defines a view model for Knockout.js. The view model data-binds to the HTML form controls in the app.

  6. Update Views/Home/Index.cshtml with the app UI. This file defines the MVC view for the home page.

  7. Enforce SSL.

    • Enable SSL for the project.

    • Add the RequireHttpsAttribute filter to the MVC pipeline.

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());    
            // New code:
            filters.Add(new RequireHttpsAttribute());
        }
      
    • Add a custom RequireHttpsAttribute filter to the Web API pipeline. (See Working with SSL in Web API.)

        config.Filters.Add(new LocalAccountsApp.Filters.RequireHttpsAttribute());
      
    • Remove AllowInsecureHttp from OAuthOptions.

        OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14)
            //AllowInsecureHttp = true
        };
      
  8. I replaced the ValuesController class with the following code, just to make the Web API responses more interesting.

     [Authorize]
     public class ValuesController : ApiController
     {
         // GET api/values
         public string Get()
         {
             var userName = this.RequestContext.Principal.Identity.Name;
             return String.Format("Hello, {0}.", userName);
         }
     }
    

    This code adds the user name to the response, which shows the request has a valid principal.

localaccountsapp's People

Contributors

ikriv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.