WebSecurity.CurrentUserId = -1 [英] WebSecurity.CurrentUserId = -1

查看:449
本文介绍了WebSecurity.CurrentUserId = -1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林开发一款Android应用,应该登录/注册,并从数据库中读取一些数据。

Im developing an android app that should login/register and read some data from database.

我的的WebAPI 登录控制器。

这是我的问题。

[HttpPost]
public HttpResponseMessage Post(Login model)
{
    if (WebSecurity.Login(model.UserName, model.Password))
    {
        int userid = WebSecurity.CurrentUserId;// <- this value is = -1
        string name = WebSecurity.CurrentUserName;// <- this value is = ""
        some query where i need the user id(userid);
    }
    else 
        ...
}

为什么成功登录后,我仍然有-1作为值?

Why after successful login i still have -1 as a value?

我注意到,即使在MVC项目同样的事情发生,但成功登录后,用户被重定向到另一个页面,数据列(在执行该控制器的当前用户ID已经被更新)。

I've noticed that even in the mvc project the same thing happens, but after the successful login the user is redirected to another page where the data is listed (when that controller is executed the current user id is already updated).

推荐答案

WebSecurity.Login设置一个cookie。该cookie必须由用户进行身份验证之前的框架,这意味着必须出现另一个Web请求重新阅读。

WebSecurity.Login sets a cookie. That cookie must be re-read by the framework before the user is authenticated, which means that another web request must occur.

在换句话说,你必须重定向到另一个页面,或其他Web请求必须进来的请求将被验证了。

In other words, you must redirect to another page, or another web request must come in before the request will be authenticated.

这篇关于WebSecurity.CurrentUserId = -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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