Advertisement
Guest User

Untitled

a guest
May 12th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. <...>
  2.         [HttpPost]
  3.         public ActionResult Logout()
  4.         {
  5.             Request.GetOwinContext()
  6.             .Authentication
  7.             .SignOut(HttpContext.GetOwinContext()
  8.                            .Authentication.GetAuthenticationTypes()
  9.                            .Select(o => o.AuthenticationType).ToArray());
  10.             return RedirectToAction("Index", "Home");
  11.         }
  12.  
  13.         public ActionResult Login(string returnUrl)
  14.         {
  15.             if (Request.IsAuthenticated)
  16.             {
  17.                 return RedirectToAction("Index", "Home");
  18.             }
  19.             ViewBag.ReturnUrl = returnUrl;
  20.             return View();
  21.         }
  22.  
  23.         [HttpPost]
  24.         public ActionResult Login(UserModel user, string returnUrl)
  25.         {
  26.             if (Request.IsAuthenticated)
  27.             {
  28.                 return RedirectToAction("Index", "Home");
  29.             }
  30.  
  31.             var username = user.Alias;
  32.             var password = user.Password;
  33.             var userMgr = new UserManager();
  34.             if (userMgr.IsValid(username, password))
  35.             {
  36.                 var ident = new ClaimsIdentity(
  37.                     new[] {
  38.                         new Claim(ClaimTypes.NameIdentifier, username),
  39.                         new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
  40.  
  41.                         new Claim(ClaimTypes.Name, username),
  42.                        
  43.                         new Claim(ClaimTypes.Role, userMgr.Role(username, password)),
  44.                     },
  45.                     DefaultAuthenticationTypes.ApplicationCookie
  46.                 );
  47.  
  48.                 HttpContext.GetOwinContext().Authentication.SignIn(
  49.                    new AuthenticationProperties { IsPersistent = false }, ident);
  50.                 if (Url.IsLocalUrl(returnUrl))
  51.                 {
  52.                     return Redirect(returnUrl);
  53.                 }
  54.                 else
  55.                 {
  56.                     return RedirectToAction("Index", "Home");
  57.                 }
  58.             }
  59.  
  60.             ModelState.AddModelError("", "Не удалось выполнить вход");
  61.             return View();
  62.         }
  63. <...>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement