您好我正在使用FormsAuthentication.RedirectFromLoginPage用户登录和重定向到default.aspx页面。 我想如果一个用户叫做管理登录被重定向到页面admin.aspxFormsAuthentication.RedirectFromLoginPage到自定义页面
这可能吗?
您好我正在使用FormsAuthentication.RedirectFromLoginPage用户登录和重定向到default.aspx页面。 我想如果一个用户叫做管理登录被重定向到页面admin.aspxFormsAuthentication.RedirectFromLoginPage到自定义页面
这可能吗?
试试这个,我认为这是你会用一个简单的解决方案中获得最接近:
FormsAuthentication.SetAuthCookie(username, true);
Response.Redirect("mypage.aspx");
如果您使用的是ASP.NET的MembershipProvider登录控制,你可以写你的逻辑在LoggedIn event
<asp:Login id="Login1" runat="server" OnLoggedIn="OnLoggedIn"></asp:Login>
protecetd void OnLoggedIn(object sender, EventArgs e)
{
if(Roles.IsUserInRole(User.Identity.Name, "Administrators"))
{
//Redirect to admin page
Response.Redirect("~/Admin.aspx");
}
}
不要忘了把一些保护上admin.aspx页藏汉,柜面有人类型直接在url中
默认行为是重定向到最初请求的资源,所以如果用户试图访问'admin.aspx'并且没有通过身份验证,用户将被发送到登录页面。成功进行身份验证后,用户将被发送到最初请求的URL(admin.aspx)。
用户 - >“admin.aspx” - > NOAUTH - >登录 - >“admin.aspx”
因此,而不是手动试图发送至某处的用户,使用这种默认行为不会为你工作?默认行为实际上是“健壮的”(可以是“admin2.aspx”,“admin3.aspx”等等......您可以拥有任意数量的“受保护资源”,并且内置过程处理所有这些资源。 ..)
验证用户
假设你已经通过我以前的上述文章了,你有一个登录页面。现在当用户单击登录按钮Authenticate
方法触发时,让我们看看该方法的代码。
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string userName = Login1.UserName;
string password = Login1.Password;
bool rememberUserName = Login1.RememberMeSet;
// for this demo purpose, I am storing user details into xml file
string dataPath = Server.MapPath("~/App_Data/UserInformation.xml");
DataSet dSet = new DataSet();
dSet.ReadXml(dataPath);
DataRow[] rows = dSet.Tables[0].Select(" UserName = '" + userName + "' AND Password = '" + password + "'");
// record validated
if (rows.Length > 0)
{
// get the role now
string roles = rows[0]["Roles"].ToString();
// Create forms authentication ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
userName, // Username to be associated with this ticket
DateTime.Now, // Date/time ticket was issued
DateTime.Now.AddMinutes(50), // Date and time the cookie will expire
rememberUserName, // if user has chcked rememebr me then create persistent cookie
roles, // store the user data, in this case roles of the user
FormsAuthentication.FormsCookiePath); // Cookie path specified in the web.config file in <Forms> tag if any.
// To give more security it is suggested to hash it
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket
// Add the cookie to the response, user browser
Response.Cookies.Add(cookie); // Get the requested page from the url
string returnUrl = Request.QueryString["ReturnUrl"];
// check if it exists, if not then redirect to default page
if (returnUrl == null) returnUrl = "~/Default.aspx";
Response.Redirect(returnUrl);
}
else // wrong username and password
{
// do nothing, Login control will automatically show the failure message
// if you are not using Login control, show the failure message explicitely
}
}
您可以通过放置核心角色名称或从数据库获取用户卷来检查它。我已经修改了这个为我的实体框架。
TestEntities entities = new TestEntities();
var user = (from s in entities.UserTables
where s.UserName == loginControl.UserName
&& s.Password == loginControl.Password
select s).SingleOrDefault();
并放置在用户角色:
user.Role
沿着这条你必须做在Global.asax一些更改文件 到目前为止我们已经设定所需的详细信息,甚至用户的窗体身份验证票现在如何在每个请求中检索这些信息,并发现请求来自哪种角色类型?为此,我们需要使用Global.asx文件的Application_AuthenticateRequest事件。请参阅下面的代码。
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// look if any security information exists for this request
if (HttpContext.Current.User != null)
{
// see if this user is authenticated, any authenticated cookie (ticket) exists for this user
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// see if the authentication is done using FormsAuthentication
if (HttpContext.Current.User.Identity is FormsIdentity)
{
// Get the roles stored for this request from the ticket
// get the identity of the user
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
// get the forms authetication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
// get the roles stored as UserData into the ticket
string[] roles = ticket.UserData.Split(',');
// create generic principal and assign it to the current request
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}
在这个连,检查是否存在用户,他/她的身份验证和TH用户的identy类型是FormsIdentity,我得到了用户的当前身份和获得我已经设定在票后Authentia的时间。一旦我获得了认证票证,我只需从票证中获取UserData并将其分割为角色(请记住,我们已将角色存储为逗号分隔值)。现在,我们拥有当前的用户角色,因此我们可以将当前用户的角色与当前身份一起传递给GenericPrincipal对象,并将其分配给curent用户对象。这将使我们能够使用IsInRole方法来检查特定用户是否属于特定角色。
如何检查用户是否具有特定角色?
要检查用户是否属于某个特定角色,请使用以下代码。如果当前记录来自经过身份验证并具有管理角色的用户,则此代码将返回true。
HttpContext.Current.User.IsInRole("admin")
如何检查用户是否通过身份验证?
要检查用户是否已通过身份验证,请使用以下代码。
HttpContext.Current.User.Identity.IsAuthenticated
要获得经过验证的用户
HttpContext.Current.User.Identity.Name
的用户名密码记住的..这代码需要在表格标记一些webconfig设置的事情:以下身份验证设置到您的网页
添加。配置文件下。
<authentication mode="Forms">
<forms defaultUrl="default.aspx" loginUrl="~/login.aspx" slidingExpiration="true" timeout="20" ></forms>
</authentication>
对于每一个用户,如果你想确保特定的文件夹,您可以将设置为他们无论是在父母的web.config文件(根文件夹),或该文件夹的web.config文件。
指定在根web.config文件(用于管理在这种情况下)的文件夹角色设置
<location path="Admin">
<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
编写代码之外,但在标签中根的web.config文件。在这里,我指定如果路径包含Admin文件夹的名称,则只允许具有“admin”角色的用户,并且所有其他用户都被拒绝。
指定在文件夹特定web.config文件的文件夹的作用的设置(在这种情况下为用户)
<system.web>
<authorization>
<allow roles="User"/>
<deny users="*"/>
</authorization>
</system.web>
写入此代码到web.config文件的用户文件夹。您可以在root的web.config文件中为用户指定设置,就像我为上面的Admin所做的那样。这只是指定设置的另一种方式。这个设置应该放在标签下。
指定设置身份验证的用户
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
写代码到安全的文件夹的web.config文件。这是指定所有匿名用户都被拒绝访问此文件夹,并且只有经过身份验证的用户才能被允许,而不管他们的角色如何。
希望这会给你一点想法解决你的问题。它对我来说工作得很好。希望你也能解决你的问题。