WebApi - 注册后才能访问CurrentUserId?

时间:2013-04-05 01:17:03

标签: authentication asp.net-mvc-4 asp.net-web-api

我有以下内容:

public HttpResponseMessage Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                    // Attempt to register the user
                    try
                    {
                        WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                        WebSecurity.Login(model.UserName, model.Password);
                        InitiateDatabaseForNewUser(model.UserName);

                        FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);

                        return Request.CreateResponse(HttpStatusCode.Created, WebSecurity.CurrentUserId);

在此代码中,CurrentUserId(最后)始终解析为-1,因为WebSecurity在方法关闭之前才会看到新用户。我想从Register()方法返回一个配置文件或至少新注册用户的id - 如何实现?

1 个答案:

答案 0 :(得分:0)

您应该能够像这样获取用户的ID:

WebSecurity.GetUserId(model.UserName)