PDA

View Full Version : مدیریت نقش ها بدون استفاده از ASP.NET identity



alirezaavini
شنبه 27 بهمن 1397, 14:29 عصر
من در پروژم مبحث identity رو از صفر نوشتم بدون استفاده از کلاس های پیش فرض ASP.NET identity
ولی الان به یه مشکلی برخوردم قصد استفاده از [Athorize] رو دارم ولی نمیدونم چطور جدول نقش هامو به برنامه معرفی کنم.

من دارای دو جدول user و role هستم
چطور میتونم از [atribute [authorize استفاده کنم.

برای راهنمایی بیشتر:





public void ConfigureServices(IServiceCollection services)
{





services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.ExpireTimeSpan = TimeSpan.FromMinutes(5000);
});
{


Login Action:

[CSHARP]
if (ModelState.IsValid)
{
User user = new User();


user = _iuser.LoginUser(login.Mobile, login.Password);


if (user != null)
{
if (user.IsActive)
{
var claims = new List<Claim>()
{
new Claim(ClaimTypes.NameIdentifier,user.Id.ToString() ),
new Claim(ClaimTypes.Name,user.Mobile)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme) ;
var principal = new ClaimsPrincipal(identity);


var properties = new AuthenticationProperties()
{
IsPersistent = login.IsRemember
};


HttpContext.SignInAsync(principal, properties);
return RedirectToAction("Index", "Profile");


}
else
{
return RedirectToAction(nameof(Active));
//راهنمایی به فعالسازی
}



از دوستان ممنون میشم راهنمایی کنن.