2

我一直在学习 ASP.Net WebApi 中的授权是如何工作的,并且我在另一篇文章(ASP.NET Web API 身份验证)中遇到了 Darin Dimitrov 的回答,我需要一些帮助来理解为什么我会得到 401。

按照 Darin 的代码,我创建了一个 WebApi 项目并添加了以下控制器和模型:

AccountController.cs

using System.Web.Http;
using System.Web.Security;
using AuthTest.Models;

namespace AuthTest.Controllers
{
    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;
        }
    }
}

用户控制器.cs

using System.Web.Http;

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

登录模型.cs

namespace AuthTest.Models
{
    public class LogOnModel
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

我创建了一个带有两个按钮和一个标签的 Web 表单应用程序,用于测试目的。

默认.aspx.cs

using System;
using System.Net.Http;
using System.Threading;

namespace AuthTestWebForms
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void ButtonAuthorizeClick(object sender, EventArgs e)
        {
            using (var httpClient = new HttpClient())
            {
                var response = httpClient.PostAsJsonAsync(
                    "http://localhost/authtest/api/account",
                    new { username = "john", password = "secret" },
                    CancellationToken.None
                ).Result;
                response.EnsureSuccessStatusCode();

                bool success = response.Content.ReadAsAsync<bool>().Result;
                if (success)
                {
                    //LabelResponse.Text = @"Credentials provided";
                    var secret = httpClient.GetStringAsync("http://localhost/authtest/api/users");
                    LabelResponse.Text = secret.Result;
                }
                else
                {
                    LabelResponse.Text = @"Sorry, you provided the wrong credentials";
                }
            }
        }

        protected void ButtonTestAuthClick(object sender, EventArgs e)
        {
            using (var httpClient = new HttpClient())
            {
                var secret = httpClient.GetStringAsync("http://localhost/authtest/api/users");
                LabelResponse.Text = secret.Result;
            }
        }
    }
}

当我单击按钮并运行 ButtonAuthorizeClick() 时,它会触发 Account 的控制器,然后触发 Users 的控制器,一切都很好。

如果我然后单击 ButtonTestAuthClick(),则会收到 401(未经授权)错误。

当我在 Chrome 或 FireFox 中寻找 ASPXAUTH cookie 时,我没有看到一个,所以我不能 100% 确定为什么 ButtonAuthorizeClick() 有效,以及我需要做什么才能使 ButtonTestAuthClick() 有效。

感谢任何人都可以帮助我。

4

3 回答 3

6

我遇到了类似的问题,虽然不是通过 Web 窗体客户端页面,而是通过 JavaScript 和 AJAX 调用。原来我已将 web.config 中的身份验证模式保留为“无”。显然,您必须在此处打开 Forms Authentication 才能使 FormsAuthentication.SetAuthCookie() 方法生效。

<authentication mode="Forms" />

一旦我解决了这个疏忽,一切都开始正常工作。:-)

于 2012-11-02T15:02:57.580 回答
1

您正在调用带有中间身份验证的 web api。为什么不通过 ajax 在客户端对用户进行身份验证?

这里的问题是每次通过 HttpClient 向 Web api 发送请求时,它实际上是由服务器处理的新 Web 请求。所有 cookie 信息都不会保留在当前请求中。为了支持这种场景,您需要自己处理cookie。

例如:如果响应有,则在 ButtonAuthorizeClick 方法中将 cookie ASPXAUTH 设置为 asp.net 标头。将 cookie ASPXAUTH 设置为 HttpRequestMessage 并由 HttpClient 发送。

Web api 最近增加了支持使用 HttpServer 创建进程内服务器,可以在当前进程中直接向当前消息处理程序发送请求。所以你可以编写如下代码:

HttpClient c = new HttpClient(new HttpServer(GlobalConfiguration.DefaultHandler));
c.GetStringAsync("http://localhost/api/Values").Wait();

在进程中发送您的请求,以便在 web api 操作中设置的 cookie 标头仍将在当前请求的管道中。签入似乎不在 RTM 版本中。你可以试试它的夜间构建http://aspnetwebstack.codeplex.com/discussions/353867

于 2012-10-12T05:58:46.827 回答
0

虽然晚了,但 ButtonTestAuthClick 中的客户端不是浏览器。它是这里的 httpClient 对象。因此,您需要以编程方式设置从其他按钮生成的 cookie。

于 2017-02-22T09:30:43.150 回答