使用 ASP.NET Web API 的跨平台身份验证 [英] Cross platform authentication using ASP.NET Web API

查看:28
本文介绍了使用 ASP.NET Web API 的跨平台身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何开始使用 ASP.NET Web API 编码身份验证,以便跨平台支持桌面、移动和 Web?我读过一些进行 RESTful 身份验证的方法,例如在标头中使用令牌.

How do I even begin coding authentication using ASP.NET Web API so it is cross-platform to support desktop, mobile and web? I'd read of some methods of doing RESTful authentication, such as using tokens in the header.

是否有使用这种方法的示例项目?

Are there any example projects out there that utilizes this method?

问题:

  1. 如果不是,我该如何修复 [Authorize] 属性以读取令牌?
  2. 如何生成此令牌?我不认为我可以使用表单身份验证,因为它使用 cookie.
  3. 我如何处理实际授权,客户端发送原始密码和用户名然后我生成令牌还是有其他方法?
  4. 我的网站在使用时如何处理?我听说这与应用使用时的处理方式不同,例如获取域和授权.

推荐答案

我认为令牌将是一个可靠的方法.表单身份验证基于 Web cookie.不过,对于所有非浏览器客户端来说,这并不是最理想的情况.

I think tokens would be a solid way to go. Forms authentication is based on cookies for the web. Not the most idea situation for all non browser clients though.

我的建议是创建一个自定义 AuthorizationFilterAttribute 并覆盖 OnAuthorization 方法.在该方法中,您可以检查是否存在您在客户端提供有效凭据后颁发给客户端的令牌.您可以在要验证的任何方法或控制器上使用此属性.这是您可以参考的示例

What I'd suggest is creating a custom AuthorizationFilterAttribute and overriding the OnAuthorization method. In that method, you could check for the existence of a token that you've issued to the client after they've supplied valid credentials. You can use this attribute on any method or controller you want validated. Here's a sample you might reference

 public class AuthorizeTokenAttribute : AuthorizationFilterAttribute 
{      
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        if (actionContext != null)
        {                
                if (!AuthorizeRequest(actionContext.ControllerContext.Request))
                {
                    actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized) { RequestMessage = actionContext.ControllerContext.Request }; 
                }
                return;
        }
    }

    private bool AuthorizeRequest(System.Net.Http.HttpRequestMessage request)
    {
        bool authorized = false;
        if (request.Headers.Contains(Constants.TOKEN_HEADER))
        {               
            var tokenValue = request.Headers.GetValues("TOKEN_HEADER");
            if (tokenValue.Count() == 1) {
                var value = tokenValue.FirstOrDefault();               
               //Token validation logic here
               //set authorized variable accordingly
            }                
        }
        return authorized;
    } }

TOKEN_HEADER 只是一个字符串,表示客户端应该为经过身份验证的请求传回的 HTTP 标头.

TOKEN_HEADER is just a string representing an HTTP header that the client should pass back for authenticated requests.

让我们来看看它

  1. 客户端请求安全数据
  2. 客户端未被授权,返回一个带有 Unauthorized 状态代码的响应
  3. 客户端发送凭据进行身份验证,应通过 HTTPS 进行保护
  4. 经过验证后,客户端会通过 HTTP 标头或任何适合您的标头接收令牌
  5. 客户端再次尝试请求安全数据,这次将令牌附加到请求中
  6. AuthorizeTokenAttribute 将验证令牌并允许执行操作.

另外,请查看 John Petersen 的这篇文章.使您的 ASP.NET Web API 安全

Also, check this post by John Petersen. Making your ASP.NET Web API’s secure

这篇关于使用 ASP.NET Web API 的跨平台身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆