[go: up one dir, main page]

0% found this document useful (0 votes)
12 views11 pages

AWD-lecture07 ASP - Net Identity

IDENTITY

Uploaded by

hayatsafi077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

AWD-lecture07 ASP - Net Identity

IDENTITY

Uploaded by

hayatsafi077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Asp.

Net Core Identity part 02

Nangarhar University Computer Science Faculty –(NUCSF) 1401/12/17


Outlines
1. Adding logging out functionally
2. Rendering view Based on authentication
3. Role Management
1. Adding new role
2. Assigning role to user
Adding logging out functionally

❑In order to logout an authenticated user we need to implement the


logout functionality of the SigninManager Class.
❑Call the SignOutAsync() method of this class.

❑await _signInManager.SignOutAsync();
Rendering view Based on authentication

➢The rendering for a user of the application may differ than another
user because of the authentication and authorization policies we have
implemented.
➢E.g: for a loged in user we do not show the login and register button
Instead we show a logout button for him.
• @using Microsoft.AspNetCore.Identity;
• @inject UserManager<ApplicationUser> userManager;
• @if (!User.Identity.IsAuthenticated){}
• Show the user name of the authenticated user.
• @userManager.GetUserName(User)
Role Management

✓In an application we have users with various roles according


to their authorization.
✓First all roles of an application should recorded and then
assign a role for a specified user.
✓For Role management Asp.net identity use the following
class.
✓ RoleManager<IdentityRole>
✓Add this class to constructor of the your class as dependency
injection.
Adding new Role.

For creating a new role add the following step:


Step1:
Add the Rolemanager Class.
private RoleManager<IdentityRole> _roleManager;
• public AccountController( RoleManager<IdentityRole> roleManager)
• {
• _roleManager = roleManager;
}
cont…

Step 2:
create a ViewModel class for role.
• public class AddRoleViewModel
• {
• [Required]
• public string Name { get; set; }
• }
cont…

• Step 3:
• Add a rezor view form to get the value of the new role.
• Step 4:
• Add a controller action method to implement creating role logic.
• public async Task<IActionResult> AddRole(AddRoleViewModel
addRoleViewModel)
• {
• if (ModelState.IsValid)
• {
• IdentityRole newRole = new IdentityRole
• {
• Name = addRoleViewModel.Name
• };
cont…

var result = await


_roleManager.CreateAsync(newRole);
if (result.Succeeded)
•{
return
RedirectToAction("UserManagement","Account");
•}
• } return View();
}
Assinging Role to user
Its your turn to ask about today’s lecture

You might also like