انواع Authentication در MVC:
1- Forms Authentication
2- Window Authentication
3- Password Authentication
شرح گزینه 1:
کاربران با ارائه مشخصات کاربری (یوزر/پسورد) در یک فرم تعیین هویت میشوند.
شرح گزینه 2:
این اهراز هویت در ارتباط با IIS و در یکی از 3 روش basic, digest, or Integrated Windows Authentication صورت میپذیرد. هنگامی که اهراز هویت IIS تمام میشود، ASP.NET از هویت اهراز شده برای تعیین و ایجاد دسترسی بهره میبرد.
شرح گزینه 3:
خدمتی پولی است که توسط MicroSoft ارائه میشود. مانند Single Sign On
در MVC دو راه برای پیادهسازی اهراز هویت وجود دارد:
1- FormsAuthentication
2- ASP.NET Identity
برای پیادهسازی FormsAuthentication مراحل زیر را انجام میدهیم:
1- جداول مربوط به User را آماده میکنیم
2- با استفاده از EF جداول را به پروژه اضافه میکنیم.
3- بیاد داشته باشید که FormsAuthentication در فضای نام System.Web.Security در دسترس میباشد.
• در Web.Config گزینه Authentication mode as Forms ست شود.
• برای لاگین از FormsAuthentication.SetAuthCookie استفاده میشود.
• برای خروج از حساب از FormAuthentication.SignOut استفاده میشود.
<authentication mode="Forms">
<forms loginUrl="Accounts/Login"></forms>
</authentication>
• کنترلهایی که باید اهراز هویت بر روی آنها صورت پذیرد را با [Authorize] مشخص نمایید.
• پس از بررسی صحت اطلاعات کاربر، کد زیر کوکی اهراز هویت را برای کاربر میسازد:
if (IsValidUser)
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Employees");
}
برای پیادهسازی Role به این شکل عمل میکنیم (راهنما):
• کلاسی میسازیم که از کلاس RoleProvider ارثبری کند:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace SecurityDemoMVC.Models
{
public class UsersRoleProvider : RoleProvider
{
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
public override string[] GetAllRoles()
{
throw new NotImplementedException();
}
public override string[] GetRolesForUser(string username)
{
using (EmployeeDBContext context = new EmployeeDBContext())
{
var userRoles = (from user in context.Users
join roleMapping in context.UserRolesMappings
on user.ID equals roleMapping.UserID
join role in context.RoleMasters
on roleMapping.RoleID equals role.ID
where user.UserName == username
select role.RollName).ToArray();
return userRoles;
}
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}
public override bool IsUserInRole(string username, string roleName)
{
throw new NotImplementedException();
}
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
}
}
پس از آن در فایل Web.Config کدهای زیر را اضافه کنید:
<roleManager defaultProvider="usersRoleProvider" enabled="true" >
<providers>
<clear/>
<add name="usersRoleProvider" type="SecurityDemoMVC.Models.UsersRoleProvider"/>
</providers>
</roleManager>
سپس در کنترل میتوان متدهایی که نیاز هست Role مناسب به آن دسترسی داشته باشد را به این شکل [Authorize(Roles="Admin")]تعیین میکنیم.