arefba
پنج شنبه 25 مهر 1392, 04:13 صبح
سلام دوستان یه مشکلی دارم که خیلی وقت بود فرصت نکرده بودم حلش کنم
من از کلاس role provider ارث بری کردم و کد زیر رو نوشتم
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace Konkoor
{
public class roleprovider:RoleProvider
{
DBKonkoorEntities context = new DBKonkoorEntities();
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
public override string[] GetAllRoles()
{
return (from u in context.TblUsers
join r in context.TblRoles on u.IDRole equals r.IDRole
select r.RoleName).ToArray();
}
public override string[] GetRolesForUser(string username)
{
return (from u in context.TblUsers
join r in context.TblRoles on u.IDRole equals r.IDRole
where u.Username == username
select r.RoleName).ToArray();
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}
public override bool IsUserInRole(string username, string roleName)
{
//using (var usersContext = new UsersContext())
//{
// var user = usersContext.Users.SingleOrDefault(u => u.UserName == username);
// if (user == null)
// return false;
// return user.UserRoles != null && user.UserRoles.Select(
// u => u.Role).Any(r => r.RoleName == roleName);
//}
var user = context.TblUsers.SingleOrDefault(u => u.Username == username);
if (user == null)
return false;
return
(from p in context.TblUsers
join r in context.TblRoles on p.IDRole equals r.IDRole
where ((p.Username==username)&&(r.RoleName==roleName))
select r.RoleName).Any();
}
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
}
}
و وب کانفیگ اصلیم رو به این صورت تنظیم کردم
-->
<configuration>
<system.web>
<httpRuntime requestValidationMode="2.0" />
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<roleManager enabled="true" defaultProvider="harchi" cacheRolesInCookie="true"
cookieName=".ASPROLES"
cookieTimeout="30"
cookiePath="/"
cookieRequireSSL="false"
cookieSlidingExpiration="true"
cookieProtection="All" >
<providers>
<clear />
<add name="harchi" type="harchi.roleprovider" />
</providers>
</roleManager>
<authentication mode="Forms">
<forms name="harchia" defaultUrl="~/Admin/Default.aspx" loginUrl="~/Login.aspx" timeout="43200" slidingExpiration="true" />
</authentication>
<sessionState timeout="30" />
</system.web>
<connectionStrings>
<add name="DBKonkoorEntities" connectionString="metadata=res://*/DBKonkoor.csdl|res://*/DBKonkoor.ssdl|res://*/DBKonkoor.msl;provider=System.Data.SqlClient;provi der connection string="Data Source=.;Initial Catalog=DBharchi;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
من تنها یک صفحه لاگین در ریشه سایتم دارم میخوام وقتی که کاربر لاگین می کنه مربوط به هر role که بود بفرستتش به اون قسمت مثلا اگه مدیر بود بفرسته به ~/Admin/Default.aspx و اگه user بود User/Default.aspx بعد از اینکه دستور زیر رو نوششتم
ool st = (from p in f.tblUsers
where ((p.Username == user) && (p.Passwords == pass))
select p).Count() != 0;
if (st == true)
{
FormsAuthentication.RedirectFromLoginPage(user, false);
}
چیکار باید کرد ؟
من از کلاس role provider ارث بری کردم و کد زیر رو نوشتم
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace Konkoor
{
public class roleprovider:RoleProvider
{
DBKonkoorEntities context = new DBKonkoorEntities();
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
public override string[] GetAllRoles()
{
return (from u in context.TblUsers
join r in context.TblRoles on u.IDRole equals r.IDRole
select r.RoleName).ToArray();
}
public override string[] GetRolesForUser(string username)
{
return (from u in context.TblUsers
join r in context.TblRoles on u.IDRole equals r.IDRole
where u.Username == username
select r.RoleName).ToArray();
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}
public override bool IsUserInRole(string username, string roleName)
{
//using (var usersContext = new UsersContext())
//{
// var user = usersContext.Users.SingleOrDefault(u => u.UserName == username);
// if (user == null)
// return false;
// return user.UserRoles != null && user.UserRoles.Select(
// u => u.Role).Any(r => r.RoleName == roleName);
//}
var user = context.TblUsers.SingleOrDefault(u => u.Username == username);
if (user == null)
return false;
return
(from p in context.TblUsers
join r in context.TblRoles on p.IDRole equals r.IDRole
where ((p.Username==username)&&(r.RoleName==roleName))
select r.RoleName).Any();
}
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
}
}
و وب کانفیگ اصلیم رو به این صورت تنظیم کردم
-->
<configuration>
<system.web>
<httpRuntime requestValidationMode="2.0" />
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<roleManager enabled="true" defaultProvider="harchi" cacheRolesInCookie="true"
cookieName=".ASPROLES"
cookieTimeout="30"
cookiePath="/"
cookieRequireSSL="false"
cookieSlidingExpiration="true"
cookieProtection="All" >
<providers>
<clear />
<add name="harchi" type="harchi.roleprovider" />
</providers>
</roleManager>
<authentication mode="Forms">
<forms name="harchia" defaultUrl="~/Admin/Default.aspx" loginUrl="~/Login.aspx" timeout="43200" slidingExpiration="true" />
</authentication>
<sessionState timeout="30" />
</system.web>
<connectionStrings>
<add name="DBKonkoorEntities" connectionString="metadata=res://*/DBKonkoor.csdl|res://*/DBKonkoor.ssdl|res://*/DBKonkoor.msl;provider=System.Data.SqlClient;provi der connection string="Data Source=.;Initial Catalog=DBharchi;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
من تنها یک صفحه لاگین در ریشه سایتم دارم میخوام وقتی که کاربر لاگین می کنه مربوط به هر role که بود بفرستتش به اون قسمت مثلا اگه مدیر بود بفرسته به ~/Admin/Default.aspx و اگه user بود User/Default.aspx بعد از اینکه دستور زیر رو نوششتم
ool st = (from p in f.tblUsers
where ((p.Username == user) && (p.Passwords == pass))
select p).Count() != 0;
if (st == true)
{
FormsAuthentication.RedirectFromLoginPage(user, false);
}
چیکار باید کرد ؟