Tại sao User.Identity.IsAuthenticated luôn sai?

lập trình


Chào mọi người,
Tôi muốn lấy tên người dùng ở bên dưới

<pre lang="c#">

 public ActionResult Login(LoginModel model, string returnUrl)
        {
 if (User.Identity.IsAuthenticated)//this is get always false
            {
string user = User.Identity.Name;//here i need username
            }
  
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password,     persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

User.Identity.IsAuthenticated always return false so userid comes always -1, I am not even able to short out my mistake, what mistake am doing and how can i overcome with this, please help me.. 

Giải pháp 1

User.Identity.IsAuthenticated sẽ không được đặt thành true cho đến khi có yêu cầu tiếp theo sau khi gọi FormsAuthentication.SetAuthCookie().

Xem http://msdn.microsoft.com/en-us/library/twk5762b.aspx

Phương thức SetAuthCookie thêm phiếu xác thực biểu mẫu vào bộ sưu tập cookie hoặc vào URL nếu CookiesSupported sai. Vé xác thực biểu mẫu cung cấp thông tin xác thực biểu mẫu cho yêu cầu tiếp theo do trình duyệt thực hiện.

Giải pháp 2

Xin chào mọi người, tôi đang thử nghiệm Xác thực tài khoản Microsoft bằng OWIN và lưu ý rằng bạn phải đặt chú thích Ủy quyền ít nhất trong một bộ điều khiển, tôi hy vọng điều này sẽ giúp ích

コメント

タイトルとURLをコピーしました