简体   繁体   中英

How to authenticate user without login

Is it possible to authenticate user under the standard ASP .NET MVC4 project using only UserId so we can skip the code below?

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
    {

Ok here is the solution

var userId = "123";

using (UsersContext db = new UsersContext())
{
   UserProfile userProfile = db.UserProfiles.FirstOrDefault(u => u.UserId == userId);
   FormsAuthentication.SetAuthCookie(userProfile.UserName, false);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM