sh
dotnet new mvc -o UserLoginApp
cd UserLoginApp
Kullanıcı Modeli Oluşturma
Models/User.cs
dosyasını oluştur ve aşağıdaki kodu ekle:
User.cs
using System.ComponentModel.DataAnnotations;
namespace UserLoginApp.Models
{
public class User
{
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
}
}
Login ViewModel'i Oluşturma
Models/LoginViewModel.cs
dosyasını oluştur ve aşağıdaki kodu ekle:
LoginViewModel.cs
using System.ComponentModel.DataAnnotations;
namespace UserLoginApp.Models
{
public class LoginViewModel
{
[Required]
[Display(Name = "Kullanıcı Adı")]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Şifre")]
public string Password { get; set; }
}
}
Kullanıcı Girişi için Controller Oluşturma
Controllers/AccountController.cs
dosyasını oluştur ve aşağıdaki kodu ekle:
AccountController.cs
using Microsoft.AspNetCore.Mvc;
using UserLoginApp.Models;
namespace UserLoginApp.Controllers
{
public class AccountController : Controller
{
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public IActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
// Bu kısımda kullanıcı doğrulama işlemleri yapılır
if (model.Username == "admin" && model.Password == "password")
{
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError(string.Empty, "Geçersiz kullanıcı adı veya şifre.");
}
return View(model);
}
}
}
Login View Oluşturma
Views/Account/Login.cshtml
dosyasını oluştur ve aşağıdaki kodu ekle:
Login.cshtml
@model UserLoginApp.Models.LoginViewModel
<h2>Kullanıcı Girişi</h2>
<form asp-action="Login" method="post">
<div>
<label asp-for="Username"></label>
<input asp-for="Username" />
<span asp-validation-for="Username" style="color:red"></span>
</div>
<div>
<label asp-for="Password"></label>
<input asp-for="Password" type="password" />
<span asp-validation-for="Password" style="color:red"></span>
</div>
<div>
<button type="submit">Giriş Yap</button>
</div>
</form>
<div style="color:red">
@Html.ValidationSummary()
</div>
Validation için Gereken Scriptleri Ekleyin
Views/Shared/_Layout.cshtml
dosyasını aç ve <body>
etiketinin sonuna şu scriptleri ekle:
_Layout.cshtml
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.10/jquery.validate.unobtrusive.min.js"></script>
Başlangıç Ayarlarını Güncelleme
Startup.cs
dosyasını aç ve aşağıdaki kodları ekle:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Uygulamayı Çalıştırma
sh
dotnet run
Tarayıcında https://localhost:5001/Account/Login
adresine giderek giriş formunu görebilirsin. Bu basit örnek, kullanıcının adı "admin" ve şifresi "password" olan bir kullanıcıyı doğrular ve geçerli girişlerde ana sayfaya yönlendirir. Gerçek bir uygulamada, kullanıcı verilerini bir veritabanında saklayıp, daha güvenli doğrulama yöntemleri kullanman gerekecektir.