Created
February 5, 2018 04:15
-
-
Save gsedubun/4f3fb947431f28c8cb503885485178f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
[Authorize] | |
public IActionResult Index() | |
{ | |
var roles = User.Identities; | |
return View(roles); | |
} | |
[Authorize(Roles="Penulis")] | |
public IActionResult About() | |
{ | |
ViewData["Message"] = "Your application description page." +User.Identity.Name; | |
return View(); | |
} | |
[Authorize(Roles="Pemabaca")] | |
public IActionResult Contact() | |
{ | |
ViewData["Message"] = "Your contact page."+User.Identity.Name;; | |
return View(); | |
} | |
public IActionResult Error() | |
{ | |
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
[AllowAnonymous] | |
[ValidateAntiForgeryToken] | |
public IActionResult Login(LoginViewModel loginViewModel,string ReturnUrl) | |
{ | |
if (ModelState.IsValid) | |
{ | |
// set claimsidentity | |
var user = Db.TblUser.SingleOrDefault(d=> d.UserName==loginViewModel.UserName && d.Password==loginViewModel.Password); | |
if(user!=null) | |
{ | |
var role = (from ur in Db.TblUserRole | |
join r in Db.TblRole on ur.TblRole equals r | |
select new {Role= r.RoleName, ur.TblUser} | |
).ToList(); | |
if(role==null) | |
return View(loginViewModel); | |
var claims =new List<Claim>(); | |
claims.Add(new Claim(ClaimTypes.NameIdentifier, loginViewModel.UserName)); | |
claims.Add(new Claim(ClaimTypes.Name, loginViewModel.UserName)); | |
claims.Add(new Claim(ClaimTypes.Email, loginViewModel.UserName)); | |
foreach (var r in role) | |
{ | |
claims.Add(new Claim(ClaimTypes.Role, r.Role)); | |
} | |
var identity = new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme); | |
var principal = new ClaimsPrincipal(identity); | |
// set authentication properties | |
var authProps = new AuthenticationProperties{ | |
IsPersistent=false, | |
}; | |
if(!string.IsNullOrEmpty(ReturnUrl)) | |
authProps.RedirectUri=ReturnUrl; | |
var s= SignIn(principal,CookieAuthenticationDefaults.AuthenticationScheme); | |
s.Properties=authProps; | |
return s; | |
} | |
else{ | |
//ModelState.AddModelError("InvalidLogin", new System.Exception("username and password is invalid.")); | |
ModelState.AddModelError(string.Empty, "Username and Password is invalid."); | |
//ViewData["Message"]="Username and Password is invalid."; | |
} | |
} | |
return View(loginViewModel); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment