WebSecurity.CurrentUserId = -1

时光总嘲笑我的痴心妄想 提交于 2019-12-11 05:42:05

问题


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:


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.



来源:https://stackoverflow.com/questions/24068329/websecurity-currentuserid-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!