【问题标题】:Implementing Forms authentication over async requests通过异步请求实现 Forms 身份验证
【发布时间】:2014-09-09 16:30:11
【问题描述】:

在带有 .net web api 后端的 Angular 应用程序中,我正在尝试通过异步请求实现表单身份验证。

这是我的 web.config 的相关部分...

<authentication mode="Forms">
  <forms loginUrl="/" cookieless="UseCookies" name=".TIMETRACK" requireSSL="false" timeout="30" protection="All" path="/TimeTrack" />
</authentication>

这是我的web api登录方法...

    [Route("Login")]
    public HttpResponseMessage Post(AppUser credentials)
    {
        var userTemplate = _authenticationProvider.GetUserByEmail(credentials.Email);

        var user = Mapper.Map<ClientUser>(credentials);
        if (userTemplate.HashCode == _cryptographyService.HashPassword(credentials.Password, userTemplate.Salt))
        {
            FormsAuthentication.SetAuthCookie(userTemplate.Email, false);
            user.IsAuthenticated = true;
        }
        return Request.CreateResponse(HttpStatusCode.OK, user);
    }

这是我进行调用的角度控制器方法...

    $scope.authenticate = function () {
        if (validateAuthentication()) {
            $http.post('Authentication/Login', { Email: $scope.email, Password: $scope.password })
                .then(loginSuccess, loginFailure);
        }
    };

使用 fiddler 分析登录后发回的响应我可以看到这个...

Set-Cookie: .TIMETRACK=4D69EB09BD2B5B1444FBF07D1AB5EEE86DDEFD237AF451EF38EF6FD78E56E24DBD01369DEC865F81297114FF354BF3BC5C6099C3C5D1D89C001014BE071B4CB5A3059E28DBC7D6B25EE27A6FE2A31E278106D78E8FE080F73A6C8BBD3B6B83F12FAE9CD1AEE80629AA72B1DD16E0606D92D0C74F8388A932930C15D89178F92A; path=/TimeTrack; HttpOnly

所以似乎正在创建 cookie。但是,在对服务器的后续请求...

User.Identity.IsAuthenticated

总是假的。我也没有看到任何 cookie 被发送回服务器的迹象。

我需要做一些特殊的事情来将该身份验证 cookie 传回服务器吗?

不管怎样,我已经在 GitHub 上发布了这个项目。你可以看看这里... https://github.com/JosephEricDavis/TimeTrack

感谢您的帮助

【问题讨论】:

  • 这不会被认为是跨域cookie吧?

标签: angularjs cookies forms-authentication asp.net-web-api asp.net-web-api2


【解决方案1】:

原来我错误地在 web.config 文件中的节点上设置了路径属性。一旦我删除了路径属性,它就开始按预期工作了。

对于像我这样不知道 cookie 上的路径是什么的人。我发现这个资源很有帮助。

Cookie path and its accessibility to subfolder pages

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2019-10-19
    • 2018-07-30
    相关资源
    最近更新 更多