角色基础应用程序ASP.NET C#(Admin,Staff .etc)Web.Config C#代码 [英] Role Base Application ASP.NET C# (Admin,Staff .etc)Web.Config C# code
本文介绍了角色基础应用程序ASP.NET C#(Admin,Staff .etc)Web.Config C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将从SQL中读取我的用户ROLE的数据但是我知道我想在从Web检查后导航.Config文件登录表单可以读取并导航到其他页面。
但是下面的代码我试过但我无法得到结果。请帮我写完整的代码。谢谢
谢谢
protected void LoginButton_Click( object sender,EventArgs e)
{
// bool str = User.IsInRole(Administrator);
/// / str = true;
if (User.IsInRole( 管理员))
{
Response.Redirect( 〜 /Administration/TaskAllocation.aspx跨度>);
}
else if (User.IsInRole(UserNameTextBox。文本))
{
Response.Redirect( 〜/ Staff / Default.aspx< /跨度>);
}
else
{
Response.Redirect( 〜/ Login.aspx);
}
}
<? xml 版本 = 1.0 ? >
< 配置 >
<! - < location path =〜/ App-Themes / CSS> - > ;
< system.web >
< 编译 debug = true targetFramework = 4.0 / >
< 身份验证 mode = 表格 >
< 表格 < span class =code-attribute> loginUrl = 〜/ Login.aspx 超时 = 60 cookieless = < span class =code-keyword> UseCookies protection = 所有 >
< / forms >
< / authentication >
< 授权 >
<! - < allow users =*/> - >
< / authorization >
< roleManager 已启用 = true cacheRolesInCookie = true >
< / roleManager >
< / system.web >
<! - < / location> - >
<! - 这里给出访问管理员文件夹和页面。 - >
< ; location path < span class =code-keyword> = 管理 >
< system.web >
< 授权 >
< allow 角色 = 管理员 / >
< deny users = * / >
< / authorization >
< / system.web >
< / location >
< location 路径 = 员工 >
< system.web >
< 授权 >
< allow 角色 = 员工 / >
< deny 用户 = * / >
< / authorization >
< / system.web >
< / location >
< / configuration >
解决方案
我将存储在SQL DB中......但是在我的C#中代码我在If条件中有写角色而
if(User.IsInRole(Administrator))
{
Response.Redirect(〜/ Administrator / TaskAllocation.aspx);
}
else
{
Response.Redirect(〜/ Default.aspx);
}
也在web.config我也有认证
< location path = 管理 >
< system.web >
< 授权 >
< allow < span class =code-attribute> roles = 管理员 / >
< deny 用户 = * / >
< / authorization >
< / system.web >
< / location >
I will read my DATA from SQL for users ROLE but know i want to Navigate after check from Web.Config File Login Form can read and navigate to other pages.
But the Below code i tried but i could not get results. Please help me to write complete procee with code.Thanks
Thanks
protected void LoginButton_Click(object sender, EventArgs e)
{
//bool str = User.IsInRole("Administrator");
////str = true;
if (User.IsInRole("Administrator"))
{
Response.Redirect("~/Administration/TaskAllocation.aspx");
}
else if (User.IsInRole(UserNameTextBox.Text))
{
Response.Redirect("~/Staff/Default.aspx");
}
else
{
Response.Redirect("~/Login.aspx");
}
}
<?xml version="1.0"?>
<configuration>
<!--<location path="~/App-Themes/CSS">-->
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" timeout="60" cookieless="UseCookies" protection="All" >
</forms>
</authentication>
<authorization>
<!--<allow users="*"/>-->
</authorization>
<roleManager enabled="true" cacheRolesInCookie="true" >
</roleManager>
</system.web>
<!--</location>-->
<!--Here is giving Access to Administrator Folder and Pages in it.-->
<location path="Administration">
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="Staff">
<system.web>
<authorization>
<allow roles="Staff"/>
<deny users= "*"/>
</authorization>
</system.web>
</location>
</configuration>
解决方案
I will store in SQL DB.....But here in my C# Code i have write role in If condition and if (User.IsInRole("Administrator")) { Response.Redirect("~/Administrator/TaskAllocation.aspx"); } else { Response.Redirect("~/Default.aspx"); } also in web.config i have also authentication <location path="Administration"> <system.web> <authorization> <allow roles="Administrator"/> <deny users= "*"/> </authorization> </system.web> </location>
这篇关于角色基础应用程序ASP.NET C#(Admin,Staff .etc)Web.Config C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文