问题描述
|
因此,一个用户正在我的应用程序(ASP .NET MVC 3)中注册。如果注册成功,则为他分配一个名为“ unreg”的角色(这意味着他的社团尚未注册)。完成注册会将他重定向到指定的操作(RegisterSociety)。如果他成功注册了他的社团,那么我就给他起“用户”角色。
无论如何,即使我的数据库正确更改(我正在使用自定义身份验证和授权),User.IsInRole(\“ user \”)也会返回FALSE。
问题在哪里和什么地方,如何解决?
更新:
我有2个表用于身份验证/授权:-用户和角色。 Roles.id_role是用户表(user.id_role)中的外键。
-我的身份验证提供者...
public class Authentication: MembershipProvider
{
public InMVC3.Models.useri createuser(string username,string password,string nume,string SCI,string NCI,string CNP,string email,int id_tip_user,out MembershipCreateStatus status)
{
useri us = new useri();
us.username = username;
us.parola = password;
us.nume = nume;
us.serie_ci = SCI;
us.nr_ci = NCI;
us.CNP = CNP;
us.email = email;
us.id_tip_user = id_tip_user;
ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username,password,true);
OnValidatingPassword(args);
if (args.Cancel)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
useri u = _user.GetUserByUsername(username);
if (u == null)
{
_user.Add(us);
status = MembershipCreateStatus.Success;
return _user.GetUserByUsername(username);
}
else
{
status = MembershipCreateStatus.DuplicateUserName;
}
return null;
}
}
-我的角色提供者
public class Autorizatie : RoleProvider
{
IUserRepository _user;
IRolRepository _rol;
public Autorizatie() : this(null) { }
public Autorizatie(IUserRepository provider)
{
_user = new UserRepository();
_rol = new RolRepository();
}
public override string[] GetRolesForUser(string username)
{
useri user = _user.GetUserByUsername(username);
tip_useri rol = _rol.GetRolById(user.id_tip_user);
string[] roles = new string[1];
roles[0] = rol.rol;
return roles;
}
public override bool IsUserInRole(string username,string roleName)
{
useri user = _user.GetUserByUsername(username);
tip_useri rol = _rol.GetRolByRoleName(roleName);
if (user != null && rol != null)
{
if (user.tip_useri.id_tip_user == rol.id_tip_user)
return true;
else return false;
}
return false;
}
public override void AddUsersToRoles(string[] usernames,string[] roleNames)
{
useri user = _user.GetUserByUsername(usernames[0]);
tip_useri rol = _rol.GetRolByRoleName(roleNames[0]);
if (user != null && rol != null)
{
user.id_tip_user = rol.id_tip_user;
_user.Update();
}
}
}
- 用户注册
[HttpPost]
public ActionResult Register(RegisterModel model)
{
Autentificare provider = (Autentificare)Membership.Provider;
IUserRepository _user = new UserRepository();
IRolRepository rol = new RolRepository();
IClientiRepository _client = new ClientiRepository();
var us = rol.GetRolByRoleName(\"unreg\").id_tip_user;
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
provider.createuser(model.UserName,model.Password,model.Nume,model.SCI,model.NCI,model.CNP,model.Email,us,out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsService.SignIn(model.UserName,false /* createPersistentCookie */);
return RedirectToAction(\"RegisterFirma\",\"Account\");
}
else
{
ModelState.AddModelError(\"\",AccountValidation.ErrorCodetoString(createStatus));
}
}
return View(model);
}
-社团登记
[HttpPost]
public ActionResult RegisterFirma(RegisterFirma client)
{
Autentificare provider = (Autentificare)Membership.Provider;
IUserRepository _user = new UserRepository();
IClientiRepository _client = new ClientiRepository();
RoleService = new Autorizatie();
clienti cl = new clienti();
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
cl.denumire = client.Firma;
cl.cod_fiscal = client.CodFiscal;
cl.reg_comert = client.Registrul;
cl.id_grupa = 1;
cl.id_localitate = Convert.ToInt32(client.Loc);
cl.adresa = client.Address;
cl.email = client.Email;
cl.telefon = client.Telefon;
cl.fax = client.Fax;
cl.pers_contact = client.PersContact;
cl.id_banca = Convert.ToInt32(client.Banca);
cl.cont_bancar = client.ContBancar;
cl.id_user = _user.GetUserByUsername(User.Identity.Name).id_user;
string[] usn = new string[1];
usn[0] = User.Identity.Name;
string[] rls = new string[1];
rls[0] = \"user\";
RoleService.AddUsersToRoles(usn,rls);
// _user.GetUserByUsername(User.Identity.Name).id_tip_user = 3;
// _user.Update();
_client.Add(cl);
FormsService.SignOut();
FormsService.SignIn(usn[0],false); -- even after sign out and sign in
return RedirectToAction(\"Index\",\"Home\");
}
catch
{
// return View(client);
return RedirectToAction(\"logon\",\"Account\");
}
}
在数据库内部,user.id_role根据\“ user \”角色进行更改。更改后的几分钟,当我再次运行应用程序时,这些角色似乎正在根据数据库运行。
更新2
<roleManager defaultProvider=\"Autorizatie\" enabled=\"true\" cacheRolesInCookie=\"false\" >
<providers>
<clear/>
<add name=\"Autorizatie\" type=\"InMVC3.Models.Autorizatie\"/>
</providers>
</roleManager>
我使用\“ true \”,然后更改为\“ false \”,并且仍然相同(对于chacheRolesInCoockie)。
解决方法
我认为原因是GetRolesForUser仅为用户返回1个角色。因此User.IsInRole将不会在字符串数组中找到第二个角色。
更新:您是对的,很抱歉,您始终只分配了1个角色。
另一个提示:以下调用做什么?
RoleService.AddUsersToRoles(usn,rls);
它会直接致电您的提供商吗?
AFAI看到,即使您不使用cookie,角色提供者基础结构也会缓存角色,但是在“ 6”静态方法中缓存设置为脏的。能否请您调用此方法?也许您直接调用了您的角色提供者,然后才将缓存设置为脏。