Advertisement
Guest User

Untitled

a guest
Dec 9th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. <div>
  2.     <label>Введите email</label><br />
  3.     <input type="email" id="email" /> <br /><br />
  4.     <label>Введите пароль</label><br />
  5.     <input type="password" id="password" /><br /><br />
  6.     <label>Подтвердите пароль</label><br />
  7.     <input type="password" id="confirmpassword" /><br /><br />
  8.     <input type="submit" id="submit" value="Регистрация" />
  9. </div>
  10.  
  11. <div class="userInfo" style="display:none;">
  12.     <p>Вы вошли как: <span class="userName"></span></p>
  13.     <input type="button" value="Выйти" id="logOut" />
  14. </div>
  15. <div class="loginForm">
  16.     <h3>Вход на сайт</h3>
  17.     <label>Введите email</label><br />
  18.     <input type="email" id="emailLogin" /> <br /><br />
  19.     <label>Введите пароль</label><br />
  20.     <input type="password" id="passwordLogin" /><br /><br />
  21.     <input type="submit" id="submitLogin" value="Логин" />
  22. </div>
  23.  
  24. @section scripts{
  25.     <script type="text/javascript">
  26.         $(function () {
  27.             $('#submit').click(function (e) {
  28.                 e.preventDefault();
  29.                 var data = {
  30.                     Email: $('#email').val(),
  31.                     Password: $('#password').val(),
  32.                     ConfirmPassword: $('#confirmpassword').val()
  33.                 };
  34.  
  35.                 $.ajax({
  36.                     type: 'POST',
  37.                     url: '/api/Account/Register',
  38.                     contentType: 'application/json; charset=utf-8',
  39.                     data: JSON.stringify(data)
  40.                 }).success(function (data) {
  41.                     alert("Регистрация пройдена");
  42.                 }).fail(function (data) {
  43.                     alert("В процесе регистрации возникла ошибка");
  44.                 });
  45.             });
  46.         })
  47.     </script>
  48.  
  49.     <script type="text/javascript">
  50.         $(function () {
  51.             //...........................
  52.  
  53.             var tokenKey = "tokenInfo";
  54.             $('#submitLogin').click(function (e) {
  55.                 e.preventDefault();
  56.                 var loginData = {
  57.                     grant_type: 'password',
  58.                     username: $('#emailLogin').val(),
  59.                     password: $('#passwordLogin').val()
  60.                 };
  61.  
  62.                 $.ajax({
  63.                     type: 'POST',
  64.                     url: '/Token',
  65.                     data: loginData
  66.                 }).success(function (data) {
  67.                     $('.userName').text(data.userName);
  68.                     $('.userInfo').css('display', 'block');
  69.                     $('.loginForm').css('display', 'none');
  70.                     // сохраняем в хранилище sessionStorage токен доступа
  71.                     sessionStorage.setItem(tokenKey, data.access_token);
  72.                     console.log(data.access_token);
  73.                 }).fail(function (data) {
  74.                     alert('При логине возникла ошибка');
  75.                 });
  76.             });
  77.  
  78.             $('#logOut').click(function (e) {
  79.                 e.preventDefault();
  80.                 sessionStorage.removeItem(tokenKey);
  81.             });
  82.         })
  83.     </script>
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement