با سلام.
من یک Custom Role Provider ساختم.
هنکامی که می خواهم یک تابع جدید بنویسم با پیغام suitable method found to override error رو برو می شوم.
لطفا راهنمایی کنید
با سلام
دوست عزیز زمانی که قصد دارید یک Provider اختصاصی بنویسید باید تمام متد های abstract رو override کنید.
باید تمام متدهای زیر رو در آن کلاس Custom خود داشته باشید
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() { throw new NotImplementedException(); } public override string[] GetRolesForUser(string username) { throw new NotImplementedException(); } public override string[] GetUsersInRole(string roleName) { throw new NotImplementedException(); } public override bool IsUserInRole(string username, string roleName) { throw new NotImplementedException(); } public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { throw new NotImplementedException(); } public override bool RoleExists(string roleName) { throw new NotImplementedException(); }
با عرض سلام و تشکر از پاسخ شما
تمام این متدها وجود دارد.
می خواهم یک متد جدید به آن اضافه کتم
لطفا راهنمایی کنید
برای اضافه کردن متد جدید هیچ مشکلی وجود ندارد
شاید در نوع و یا بدنه کدتون مشکلی وجود دارد
کدتون رو قرار دهید تا بررسی کنیم
با تشکر
کد زیر مربوط به custom role provider هست:
using System; using System.Collections.Generic; using System.Configuration.Provider; using System.Linq; using System.Web; using System.Web.Security; using Talash.Models; using System.Web.Mvc; namespace Talash { public class CustomRoleProvider : RoleProvider { talasheduEntities talash = new talasheduEntities(); public override string ApplicationName { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public override void AddUsersToRoles(string[] usernames, string[] roleNames) { throw new NotImplementedException(); } public override void CreateRole(string roleName) { if (RoleExists(roleName)) { throw new ProviderException("Role name already exists."); } else { Tbl_Role r = new Tbl_Role(); r.RoleName = roleName; talash.Tbl_Role.Add(r); talash.SaveChanges(); }//throw new NotImplementedException(); } public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { Tbl_Role r = talash.Tbl_Role.First(i=>i.RoleName==roleName); talash.Tbl_Role.Remove(r); throw new NotImplementedException(); } public override string[] FindUsersInRole(string roleName, string usernameToMatch) { throw new NotImplementedException(); } public override string[] GetAllRoles() { string[] f = { "" }; var d = (from u in talash.Tbl_Role select new { u.RoleName }); for (int i = 0; i < d.Count(); i++) { f[i] = d.ToArray()[i].RoleName; } return f; } public override string[] GetRolesForUser(string username) { string[] f = {""}; List<string> userRoles = new List<string>(); var d = from u in talash.Tbl_User join k in talash.Tbl_RoleInUser on u.UserID equals k.UserID join r in talash.Tbl_Role on k.RoleID equals r.RoleID where u.MelliCode== username select new { r.RoleName }; for (int i = 0; i < d.Count(); i++) { userRoles.Add(d.ToArray()[i].RoleName.Trim()); } return userRoles.ToArray(); } public override string[] GetUsersInRole(string roleName) { throw new NotImplementedException(); } public override bool IsUserInRole(string username, string roleName) { bool userIsInRole = false; var userID = talash.Tbl_User.SingleOrDefault(u => u.MelliCode == username); var rolename = (from r in talash.Tbl_Role join ru in talash.Tbl_RoleInUser on r.RoleID equals ru.RoleID where ru.UserID == userID.UserID && r.RoleName == roleName select new { r.RoleName }).ToArray(); if (rolename.Count() > 0) { userIsInRole = true; } return userIsInRole; } public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { throw new NotImplementedException(); } public override bool RoleExists(string roleName) { bool exists = false; var role = from r in talash.Tbl_Role where r.RoleName == roleName select r.RoleID; if (role.Count()>0) { exists = true; } return exists; } } }
متد زیر را که اضافه می کنم روی این متد ارور می گیرد و پیغامی که گفتم می دهد:
public override bool IsUser(string username) { string[] f = { "" }; bool chk = false; List<string> userRoles = new List<string return chk; }
لطفا راهنمایی کنید
دوست عزیز کلمه کلیدی override رو از متدتون حذف کنید
دوست عزیز.
حذف کردم درست شد.
مشکل اینه که وقتی Roles رو می نویسم متد جدیدی که ساخته شده تو لیست متدهاش نیست
لطفا راهنمایی کنید
public static bool IsUser(string username) { string[] f = { "" }; bool chk = false; List<string> userRoles = new List<string return chk; }
جناب مدائنی
مشکل جدید اینه که وقتی Roles رو می نویسم متد جدیدی که ساخته شده تو لیست متدهاش نیست
لطفا راهنمایی کنید
دوست عزیز متد رو به صورت زیر تعریف کنید
public bool IsUser(string username) { string[] f = { "" }; bool chk = false; List<string> userRoles = new List<string>(); return chk; }
و در برنامه به صورت زیر استفاده کنید
var CustomProvider = Roles.Provider as CustomRoleProvider; CustomProvider.IsUser("UserName");
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)