使用 Identity 2.0 在 Web 中进行基于角色的安全授权
Role-based Security Authorization in web froms using Identity 2.0
我看到的例子不是上百个,而是上千个,其中从头到尾完成了使用 MVC identity 2.0 的示例,但没有一个带有该死的 Web 表单的示例,而存在的示例甚至不值得,因为它只是非常基本的.
我正在开发一个应用程序,其中我有三个角色,用户、管理员、超级用户,所有这些都在 AspNetRoles table 中,因为我使用的是身份 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 table 获得了角色,这就是我获得这些角色的方式
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
拳头你的角色应该是一些文本值,因为它不作为id
将此对象或模型保存在 `db.saveChanges(); 之上之后;
你最终要将这个角色添加到 aspnetroles table 并且你将如何做到这一点非常简单,只需一行
// 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
我希望这对你完全有帮助
我看到的例子不是上百个,而是上千个,其中从头到尾完成了使用 MVC identity 2.0 的示例,但没有一个带有该死的 Web 表单的示例,而存在的示例甚至不值得,因为它只是非常基本的. 我正在开发一个应用程序,其中我有三个角色,用户、管理员、超级用户,所有这些都在 AspNetRoles table 中,因为我使用的是身份 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 table 获得了角色,这就是我获得这些角色的方式
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
拳头你的角色应该是一些文本值,因为它不作为id 将此对象或模型保存在 `db.saveChanges(); 之上之后; 你最终要将这个角色添加到 aspnetroles table 并且你将如何做到这一点非常简单,只需一行
// 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
我希望这对你完全有帮助