how can i display user full information after login in mvc 5.
I want perfect coding for this.. where step by step i can follow..
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Munesh SharmaPosted May 2, 2016, 2:28 PM
Amrut SablePosted Apr 25, 2016, 12:55 AM
Manish Kumar ChoudharyPosted Jan 6, 2015, 7:19 AM
shreya kumrawatPosted Jan 6, 2015, 7:17 AM
Manish Kumar ChoudharyPosted Jan 6, 2015, 4:31 AM
This is controller code :-
while login Use private void SignInAsync(User User) to set user information to access after successful login
private HrDbContext db = new HrDbContext();
//GET: Employee/Login
[HttpGet]
public ActionResult Index(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
// POST: /Account/Login
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(EmployeeLoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
User result = db.Users.Include("Employee").Where
(
x => x.UserName == model.UserId
&&
x.Password == model.Password
&&
x.IsActive == true
).FirstOrDefault();
if (result != null)
{
//Sture Role Id for creating a dynimic menu after successfull login
ViewBag.RoleId = result.RoleId;
//Update the last Inserted Date
result.LastAccess = DateTime.Now;
db.Users.Attach(result);
var entry = db.Entry(result);
entry.Property(e => e.LastAccess).IsModified = true;
// other changed properties
db.SaveChanges();
SignInAsync(result);
return RedirectToAction("Welcome");
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
return View();
}
private void SignInAsync(User User)
{
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, User.Employee.Name));
claims.Add(new Claim(ClaimTypes.Email, User.Employee.EmailId));
claims.Add(new Claim(ClaimTypes.Role, User.RoleId.ToString()));
var id = new ClaimsIdentity(claims,
DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(id);
}
[Authorize]
public ActionResult Welcome()
{
return View();
}
//
// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index");
}
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
User Following code to display the user information
In view :- use something like
User.Identity.GetUserName()
In controller Use something like
User. Name
User .Role
decorate the controller with
[Authorize] then only you can access those controller after login.
Manish Kumar ChoudharyPosted Jan 6, 2015, 4:26 AM
shreya kumrawatPosted Jan 6, 2015, 4:15 AM
Manish Kumar ChoudharyPosted Jan 5, 2015, 11:12 PM
Hi shreya kumrawat,
Go through following links it will help you.
http://stackoverflow.com/questions/18448637/how-to-get-current-user-and-how-to-use-user-class-in-mvc5
http://stackoverflow.com/questions/26739778/get-userid-of-logged-in-user-in-asp-net-mvc-5
http://www.jaysmith.us/post/Display-User-Full-Name-using-Windows-Authentication-In-MVC.aspx