在WCF和&amp ;;之间共享FormsAuthentication Cookie ASP.NET应用程序

时间:2016-11-18 15:58:01

标签: asp.net wcf cookies forms-authentication

为了额外的安全性,我在我的WCF服务中生成FormsAuthentication cookie并将其返回到我的Web应用程序,但是我想在WCF上向特定服务发出请求时使用该cookie。如何将此配置为与每个请求一起传递?我已经设置了机器和解密密钥,但每当我检查cookie到达WCF服务时,都没有找到cookie。

我已经尝试了各种方法将cookie添加到标题中,但是只是执行以下操作似乎是有意义的,因为它生成并将其添加到标题中。

Return FormsAuthentication.SetAuthCookie(email, False)

然后当我使用...检查客户端上的cookie时

HttpContext.Current.Response.Cookies

它是空的。

我在客户端做错了什么,我正在尝试的是以下内容。

If AccountService.Authenticate(model.Email, model.Password) Then
    Return RedirectToAction("Index", "Home")
End If

希望由于对该服务的调用,它只会在标题中获取cookie。

我目前正以一种不太理想的方式解决这个问题,在服务请求正文中返回cookie

服务器

If CheckPassword(password, Unicode.GetString(user.Password)) And user.EmailConfirmed Then
    Return FormsAuthentication.GetAuthCookie(email, False)
End If

客户端

Dim cookie As HttpCookie = AccountService.Authenticate(model.Email, model.Password)
If cookie IsNot Nothing Then
    HttpContext.Response.Cookies.Add(cookie)
    Return RedirectToAction("Index", "Home")
End If

验证方法(WCF)

Public Function Authenticate(email As String, password As String) As Boolean Implements IAccountService.Authenticate
    Dim user As [Public] = db.Publics.Where(
        Function(i) i.Email.ToLower = email.ToLower
    ).FirstOrDefault
    If Not IsNothing(user) Then
        If CheckPassword(password, Unicode.GetString(user.Password)) And user.EmailConfirmed Then
            Dim cookie As HttpCookie = FormsAuthentication.GetAuthCookie(email, False)
            HttpContext.Current.Response.Cookies.Add(cookie)
            Return True
        End If
    End If
    Return False
End Function

0 个答案:

没有答案