WebSecurity.CurrentUserId = -1

2019-07-27 05:31发布

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

I have WebApi login controller.

and this is my problem

[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 
        ...
}

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

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).

1条回答
戒情不戒烟
2楼-- · 2019-07-27 06:25

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.

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

查看更多
登录 后发表回答