最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了。

Demo

一、FORM Authentication

1、新建asp.net 空项目->Web API,如下图所示:

Asp.net  Web Api 2  FORM Authentication Demo

2、先创建一个简单无认证示例:

    (1)、Models文件夹下新建Product类,

    /// <summary>
    ///  产品
    /// </summary>
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

    (2)、Controllers文件夹下新建ProductsController类,

 public class ProductsController : ApiController
    {
        Product[] products = new Product[] 
        { 
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
        };


        public IEnumerable<Product> GetAll()
        {

            return products;
        }


        public IHttpActionResult Get(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }

    }

   (3)、创建index.html页面,前端脚本如下,   

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Web API2 Studt</title>

</head>
<body>

    <div>
        <h2>All Products</h2>
        <ul id="products"></ul>
    </div>
    <div>
        <h2>Search by ID</h2>
        <input type="text" id="prodId" size="5" />
        <input type="button" value="Search" onclick="find();" />
     
        <p id="product" />
    </div>
    <script src="JS/jquery-2.0.3.min.js"></script>
    <script>


        var uri = 'api/products';


        $(document).ready(function () {
            // Send an AJAX request
            $.getJSON(uri)
                .done(function (data) {
                    // On success, 'data' contains a list of products.
                    $.each(data, function (key, item) {
                        // Add a list item for the product.
                        $('<li>', { text: formatItem(item) }).appendTo($('#products'));
                    });
                }).fail(function (jqXHR, textStatus, err) {
                   
                    if (err == 'Forbidden')
                        {self.location = 'login.html';}

            });
        });

        function formatItem(item) {
            return item.Name + ': $' + item.Price;
        }

        function find() {

            var id = $('#prodId').val();
            $.getJSON(uri + '/' + id)
                .done(function (data) {
                    $('#product').text(formatItem(data));
                })
                .fail(function (jqXHR, textStatus, err) {
                    $('#product').text('Error: ' + err);
                });
        }


    </script>
</body>
</html>
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2021-10-21
  • 2021-10-27
  • 2021-10-05
猜你喜欢
  • 2021-10-29
  • 2021-10-02
  • 2021-10-28
  • 2021-09-18
  • 2021-05-18
  • 2022-02-09
  • 2021-12-25
相关资源
相似解决方案