Passing additional data in WebSecurity

2019-08-06 11:36发布

I'm using default forms authentication implementation in ASP.NET MVC 4.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
    {
       //Assign additional data
        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);
}

This works fine. I need to pass some additional data of each logged in user, to use later, without accessing the database again. (ex: user roles and role details)

Is there any implementation in WebSecurity to do this without using Session variable?

2条回答
狗以群分
2楼-- · 2019-08-06 11:39

you can use this method to additional values and parameter websecurity method :

WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
                        propertyValues: new
                        {
                            NameFamily = model.NameFamily,
                            PostCode = model.PostCode,
                            Address = model.Address,
                            Phone = model.Phone,
                            Lat = model.Lat,
                            Lng = model.Lng
                        });
查看更多
姐就是有狂的资本
3楼-- · 2019-08-06 11:58

Typically you would have additional values to be stored at the time of user registration using the code

WebSecurity.CreateUserAndAccount("username", "somepassword", new{DateCreated=DateTime.Now});

You can add multiple properties in the anonymous object

WebSecurity.Login takes only 2 parameters to login an existing user

查看更多
登录 后发表回答