2015-10-29 80 views
-1

上的用戶分配角色我使用ASP.NET 4.5模板創建了MVC Web應用程序。在Web.config中,我將SQL連接字符串更改爲指向現有的SQL Server數據庫。如何爲ASP.NET 4.5 MVC應用程序

我運行該項目並創建了必要的表到我的SQL Server數據庫中。我也創建一個新用戶。

我的第一個問題是如何創建一個用戶角色? 而我的第二個問題是如何使用「角色」給予我的用戶尊重我的應用程序的某些部分的權限?

親切的問候,

enter image description here

enter image description here

enter image description here

+0

http://www.asp.net/是開始教程和操作指南的地方 –

+0

謝謝@羅曼。是否有可能爲我的問題分享特定的鏈接。 – NTMS

回答

0

確定。以下是如何創建角色。

我正在使用VS2015和ASP.NET 4.5 MVC。我使用Microsoft Asp.Net 4.5標識進行身份驗證。

context.Roles.Add(新icrosoft.AspNet.Identity.EntityFramework.IdentityRole()

所以在我的模型Role.cs

// POST: /Roles/Create 
[HttpPost] 
public ActionResult Create(FormCollection collection) 
{ 
    try 
    { 
     context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole() 
     { 
      Name = collection["RoleName"] 
     }); 
     context.SaveChanges(); 
     ViewBag.ResultMessage = "Role created successfully !"; 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

而且我也分別放在我的每個控制器的代碼頂部:

[Authorize(Roles = "TestRole")] 
相關問題