扩展表单身份验证以使用自定义http标头作为故障单

问题描述 投票:6回答:2

我有一个wcf webhttp服务,它使用表单身份验证来验证用户。如果票证来自cookie集合或URL,则此方法可以正常工作。

但是现在我想在自定义http标头中发送表单auth标签的字符串,并更改表单auth模块以检查该标头而不是cookie。

我认为扩展表单auth以实现这一点应该很容易,但是找不到任何如何的资源。你能为我指出正确的方向吗 ?

这是我的身份验证流程如何工作,

  1. 客户端使用用户名和密码调用authenticate方法
  2. 服务返回加密的票证字符串
  3. 客户端在每个后续请求的http头中发送收到的票证字符串
  4. 服务检查auth标头并验证身份验证票证
asp.net forms-authentication webhttp
2个回答
3
投票

FormAuthentication模块不可扩展,但您可以编写自己的身份验证。这很简单:

认证(2):


var formsTicket = new FormsAuthenticationTicket(
    1, login, DateTime.Now, DateTime.Now.AddYears(1), persistent, String.Empty);
var encryptedFormsTicket = FormsAuthentication.Encrypt(formsTicket);
//return encryptedFormsTicket string to client

带附加票的服务电话(4):


var ticket = FormsAuthentication.Decrypt(encryptedFormsTicket)
//extract authentication info from ticket: ticket.Name

1
投票

我不确定这是要走的路(优雅方面),但是如何在global.asax.cs中为Application BeginRequest添加一个事件并从头部获取字符串并自己将cookie注入Request中(Forms身份验证应该如此)然后选择那个)。

就像是:


protected void Application_BeginRequest()
{
    // Your code here to read request header into cookieText variable
    string cookieText = ReadCookieFromHeader();

    var cookieData = FormsAuthentication.Decrypt(cookieText);

    if (!cookieData.Expired)
    {
        HttpContext.Current.Request.Cookies.Add(new HttpCookie(cookieData.Name, cookieText));
    }
}

免责声明:请注意,我没有对此进行测试,只是按照您的方式进行测试!

© www.soinside.com 2019 - 2024. All rights reserved.