본문 바로가기

C#

C#- ASP.NET 으로 웹 로그인 화면 만들기(MVC-모델-뷰-컨트롤러) !

반응형

일단 ASP.NET 설치를 해야한다!

맴버쉽 컨트롤러(

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.Web.CodeGeneration.Contracts.Messaging;
using NetCore.Web.Models;

namespace NetCore.Web.Controllers
{
    public class MembershipController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Login(LoginInfo login)
        {
            string message = string.Empty;
            if (ModelState.IsValid)
            {
                string userid = "ㅁㅁㅁㅁㅁ";
                string password = "ㅁㅁㅁㅁ";

                if (login.UserId.Equals(userid) && login.Password.Equals(password))
                {
                    TempData["Message"] = "로그인이 성공적으로 이루어졌습니다.";
                    return RedirectToAction("Index", "Membership");
                }
                else
                {
                    message = "로그인되지 않았습니다";
                }
            }
            else
            {
                message = "로그인정보를 올바르게 입력하세요.";
            }
            ModelState.AddModelError(string.Empty, message);
            return View(login);
        }
    }
}

 

로그인 모델

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace NetCore.Web.Models
{
    public class LoginInfo
    {
        [Required(ErrorMessage ="사용자 아이디를 입력하세요")]
        [MinLength(6,ErrorMessage = "사용자 아이디는 6자 이상 입력하세요")]
        [Display(Name = "사용자 아이디")]
        public string UserId { get; set; }

        [Required(ErrorMessage = "비밀번호를 입력하세요")]
        [MinLength(6, ErrorMessage = "비밀번호는 6자 이상 입력하세요")]
        [Display(Name = "비밀번호")]
        public string Password { get; set; }

    }
}

뷰 추가 

Index -view

@{
    ViewData["Title"] = "멤버십";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>@ViewData["Title"]</h1>

<div class="text-info">@TempData["Message"]</div>

Login -view

@model LoginInfo
@{
    ViewData["Title"] = "멤버십 로그인";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>@ViewData["Title"]</h1>

<div class="row">
    <div class="col-md-4">
        <form method="Post" asp-controller="Membership" asp-action="Login" onsubmit="btnLogin.disabled = true; return true;">
            <div class="text-danger" asp-validation-summary="All"></div>
            <div class="form-group">
                @Html.DisplayNameFor(m => m.UserId)
                <input type="text" class="form-control" asp-for="UserId" placeholder="User id" autofocus />
                <span class="text-danger" asp-validation-for="UserId"></span>
            </div>
            <div class="form-group">
                @Html.DisplayNameFor(m => m.Password)
                <input type="password" class="form-control" asp-for="Password" placeholder="Password" />
                <span class="text-danger" asp-validation-for="Password"></span>
            </div>
            <button type="submit" name="btnLogin" class="btn btn-lg btn-success btn-block">멤버십 로그인</button>
        </form>  
    </div>
</div>

반응형