簡體   English   中英

使用Identity 2.0的Web froms中基於角色的安全授權

[英]Role-based Security Authorization in web froms using Identity 2.0

我沒有看到數百個示例,但從頭到尾都完成了具有MVC身份2.0的完整示例,但是沒有一個帶有血腥的Web表單的示例,並且存在的一個示例即使非常基礎也並不值得。 我正在開發一個具有三個角色(用戶,管理員,超級用戶)的應用程序,所有這些角色都在AspNetRoles表中,因為我正在使用身份2.0。 現在,當我創建一個用戶時,我也為該用戶分配了這些角色之一。 在擔任此角色和工作之前,我曾從事過用於桌面應用程序的自定義角色系統的工作。 所以在這里,我嘗試了所有在CodeProject上寫的有關表單身份驗證的鏈接和文章,以及我們可以在web.config中執行的所有操作,但沒有任何幫助。請看一下此屏幕快照http://prntscr.com/6ca09i,您可能會得到有點主意,我的意思是。 我在注冊時的C#代碼是

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //owin entity
        var userStore = new UserStore<IdentityUser>();
        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager
            .ConnectionStrings["GCR"].ConnectionString;

        var manager = new UserManager<IdentityUser>(userStore);
        //string  userInfor;// = new UserInformation();

        // check if the url contains an id perameter
        if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
        {
            var id = Convert.ToInt32(Request.QueryString["id"]);
            var userInfo = new UserInformation
            {
                Email = txtEmail.Text,
                FirstName = txtFirstName.Text,
                LastName = txtLastName.Text,
                AddressLine1 = txtAddressLine1.Text,
                AddressLine2 = txtAddressLine2.Text,
                City = txtCity.Text,
                State = ddlState.SelectedValue,
                ZipCode = Convert.ToInt32(txtZip.Text),
                PhoneNumber = txtPhone.Text,
                RoleId = Convert.ToInt32(ddlRole.SelectedValue)

這是我的注冊頁面,我在其中分配實際上沒有得到分配的角色http://prntscr.com/6ca1xi

現在,請告訴我如何創建基於角色的應用程序,其中在一個文件夾中我們擁有不同文件,具有不同角色的用戶可以訪問該文件。我已經在Identity上浪費了兩天,我也不想浪費更多時間

我從aspnetroles表中獲得了角色,這就是我獲得這些角色的方式

var context = new ApplicationDbContext();
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);

if (User.IsInRole("admin"))
{
    //come here
}

這就是您要如何正確處理此問題的方法

        var userInfo = new UserInformation
        {
            Email = txtEmail.Text,
            FirstName = txtFirstName.Text,
            LastName = txtLastName.Text,
            AddressLine1 = txtAddressLine1.Text,
            AddressLine2 = txtAddressLine2.Text,
            City = txtCity.Text,
            State = ddlState.SelectedValue,
            ZipCode = Convert.ToInt32(txtZip.Text),
            PhoneNumber = txtPhone.Text,
            RoleId = ddlRole.SelectedValue

首先,您的角色應該是一些文本值,因為在`db.saveChanges();中保存該對象或模型后,它不會將角色用作id。 您最終將把這個角色添加到aspnetroles表中,而如何做這很簡單,只需一行

 // add role to the user which is created right now 
  manager.AddToRole(userInfo.GUID, ddlRole.Text.Trim());

第一個參數是用戶的ID,第二個參數是下拉菜單,您可以在其中選擇用戶角色,也可以使該角色存在於其中。 現在,您要如何檢查這一頁加載非常簡單,就像這樣

#region page load
            if (!IsPostBack)
            {
                if (User.IsInRole("admin") || User.IsInRole("superuser"))
                {

                }
                else
                {
                    string unAuthorizedRedirect = WebConfigurationManager.AppSettings["UnAuthorizedRedirect"];
                    Response.Redirect("~/" + unAuthorizedRedirect);
                }
            }
            #endregion

希望這對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM