15-mavzu: Veb autentifikatsiya va identifikatsiya


Rollarni yaratish va o‘chirish



Download 1,68 Mb.
bet8/14
Sana09.06.2022
Hajmi1,68 Mb.
#646265
1   ...   4   5   6   7   8   9   10   11   ...   14
Bog'liq
autentifikatsiya, avtorizatsiya, seanslar

Rollarni yaratish va o‘chirish
RoleAdminController-ni quyidagi misolda ko‘rsatilgan kod bilan loyihaga qo‘shing:
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using System.ComponentModel.DataAnnotations;
using Users.Infrastructure;
using Users.Models;

namespace Users.Controllers


{
public class RoleAdminController : Controller
{
private AppUserManager UserManager
{
get
{
return HttpContext.GetOwinContext().GetUserManager();
}
}

private AppRoleManager RoleManager


{
get
{
return HttpContext.GetOwinContext().GetUserManager();
}
}

public ActionResult Index()


{
return View(RoleManager.Roles);
}

public ActionResult Create()


{
return View();
}

[HttpPost]


public async Task Create([Required] string name)
{
if (ModelState.IsValid)
{
IdentityResult result
= await RoleManager.CreateAsync(new AppRole(name));

if (result.Succeeded)


{
return RedirectToAction("Index");
}
else
{
AddErrorsFromResult(result);
}
}
return View(name);
}

[HttpPost]


public async Task Delete(string id)
{
AppRole role = await RoleManager.FindByIdAsync(id);
if (role != null)
{
IdentityResult result = await RoleManager.DeleteAsync(role);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
else
{
return View("Error", result.Errors);
}
}
else
{
return View("Error", new string[] { "Rol topilmadi." });
}
}

private void AddErrorsFromResult(IdentityResult result)


{
foreach (string error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
}
}

IdentityHelpers.cs klass faylini quyidagi tarkib bilan “Infrastructure” papkasiga qo‘shing:


using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity.Owin;

namespace Users.Infrastructure


{
public static class IdentityHelpers
{
public static MvcHtmlString GetUserName(this HtmlHelper html, string id)
{
AppUserManager mgr = HttpContext.Current
.GetOwinContext().GetUserManager();
return new MvcHtmlString(mgr.FindByIdAsync(id).Result.UserName);
}
}
}

Quyidagi misol “/Views/RoleAdmin” papkasida joylashgan “Index.cshtml” faylining tarkibini ko‘rsatadi:


@using Users.Models
@using Users.Infrastructure
@model IEnumerable

@{
ViewBag.Title = "Rollar";


}



Rollar








@if (Model.Count() == 0)
{



}
else
{
foreach (AppRole role in Model)
{






}
}
ID Nomi Foydalanuvchilar
Rol yo’q
@role.Id @role.Name
@if (role.Users == null || role.Users.Count == 0)
{
Ushbu rolga bironta ham foydalanuvchi biriktirilmagan.
}
else
{


@string.Join(", ", role.Users.Select(x =>
Html.GetUserName(x.UserId)))


}

@using (Html.BeginForm("Delete", "RoleAdmin", new { id = role.Id }))
{
@Html.ActionLink("Tahrirlash", "Edit", new { id = role.Id },
new { @class = "btn btn-primary btn-xs", style = "float:left; margin-right:5px" })

}


@Html.ActionLink("Yaratish", "Create", null, new { @class = "btn btn-primary" })



Quyidagi misol yangi rollarni yaratish uchun ishlatiladigan “Create.cshtml” View-ni o‘z ichiga oladi:


@model string
@{
ViewBag.Title = "Rolni yaratish";
}


Download 1,68 Mb.

Do'stlaringiz bilan baham:
1   ...   4   5   6   7   8   9   10   11   ...   14




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish