添加默认的角色,以新注册的用户VS防爆$ P $ 2013 PSS [英] Adding default role to newly registered user in VS Express 2013
问题描述
我开始用做在用户的名字和姓氏添加补充创建Register.aspx.cs默认值。这工作得很好。
我现在希望能够为默认的任何新用户用户的角色,使他们只能访问相应的网页。我看到在旧版本的VS很多职位,,,但没有似乎在这里工作。我已经添加了两个角色的AspNetRoles表(管理员,用户)。
我是新来的VS和C#,所以我可能失去了一些东西简单。
当前code:
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
使用Microsoft.AspNet.Identity.Owin;
使用系统;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用WebApplication1.Models;命名空间WebApplication1.Account
{
公共部分类寄存器:页
{
保护无效CreateUser_Click(对象发件人,EventArgs的发送)
{
VAR经理=新的UserManager();
VAR用户=新ApplicationUser()
{
用户名= UserName.Text,
名字= FirstName.Text,
姓氏= LastName.Text
};
IdentityResult结果= manager.Create(用户,Password.Text);
如果(result.Succeeded)
{
IdentityHelper.SignIn(经理,用户,isPersistent:假);
IdentityHelper.RedirectToReturnUrl(的Request.QueryString [RETURNURL],响应);
}
其他
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
}
}
检查用户的角色,当他们登录。
我不肯定,但把我的头顶部的我相信新的用户默认分配给任何角色都没有。所以它应该很容易检查什么他们在角色,决定他们是新用户,然后将它们分配到的角色,你想他们是在
<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.security.roles.addusertorole(v=vs.110).aspx\" rel=\"nofollow\">https://msdn.microsoft.com/en-us/library/system.web.security.roles.addusertorole(v=vs.110).aspx
<一个href=\"http://www.asp.net/web-forms/overview/older-versions-security/roles/assigning-roles-to-users-cs\" rel=\"nofollow\">http://www.asp.net/web-forms/overview/older-versions-security/roles/assigning-roles-to-users-cs
使用的System.Web;
使用System.Web.Security; //添加此导入命名空间
Roles.AddUserToRole(用户名,角色名)
I am starting with the default created Register.aspx.cs with additions made to add in the FirstName and LastName of the user. This works fine.
I now want to be able to default any new users to a role of "User" so that they can only access appropriate pages. I see lots of posts on older versions of VS,,, but none seem to work here. I have added 2 roles to the AspNetRoles Table (Admin,User).
I am new to VS and C#, so likely am missing something simple.
Current Code:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using WebApplication1.Models;
namespace WebApplication1.Account
{
public partial class Register : Page
{
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = new UserManager();
var user = new ApplicationUser()
{
UserName = UserName.Text,
FirstName = FirstName.Text,
LastName = LastName.Text
};
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
IdentityHelper.SignIn(manager, user, isPersistent: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
}
}
Check the user's role when they sign in.
I am not for sure, but off of the top of my head I believe new users are by default assigned to no roles at all. So it should be easy to check what roles they are in, decide they are a new user, and then assign them to the roles you would like them to be in.
https://msdn.microsoft.com/en-us/library/system.web.security.roles.addusertorole(v=vs.110).aspx
http://www.asp.net/web-forms/overview/older-versions-security/roles/assigning-roles-to-users-cs
using System.Web;
using System.Web.Security; // Add this to import the namespace
Roles.AddUserToRole(userName, roleName)
这篇关于添加默认的角色,以新注册的用户VS防爆$ P $ 2013 PSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!