如果声明没有给出理想的结果 [英] if satement not giving desired result

查看:78
本文介绍了如果声明没有给出理想的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net应用程序中收到以下错误

用户'a'已经不在角色'临时'



我得到此错误的相关代码部分是(第29行)



第27行:if(Roles.IsUserInRole(临时))

第28行:{

第29行:Roles.RemoveUserFromRole(用户名,临时);

第30行:Roles.AddUserToRole(用户名,临时1 );

第31行:Response.Redirect(〜/ Profiles / ProfileBasics.aspx?);

第32行}



由于用户不属于临时角色,因此服务器应该忽略这部分代码。



我想只在用户处于临时角色时执行此代码,否则其他代码可以正常工作。但是,当用户处于临时角色时,我总是会收到此错误。



请帮助我克服此错误。



我是asp.net的新手,我正在使用C#



非常感谢帮助

解决方案

如果没有剩下的代码,就无法给出确切的答案,但是......

您的if测试是否检查 当前 用户是一个角色,如果他继续删除: Roles.IsUserInRole [ ^ ]

删除使用您传递给方法的用户名: Roles.RemoveUserFromRole [ ^ ], 当前用户。



因此,如果当前登录的用户(即you)处于临时角色,然后您将尝试从临时中移除用户名变量中具有用户名的人。



我猜测当前登录的用户与用户名中的用户不一样...使用调试器并检查!


在解决方案中解释 OriginalGriff

Roles.IsUserInRole(临时)=>检查当前登录用户

http://msdn.microsoft.com/en-us/library/4z6b5d42(v=vs.110).aspx



Roles.IsUserInRole(UserName,临时)=>检查指定用户是否在该角色中。



http:/ /msdn.microsoft.com/en-us/library/bz1zy88e(v=vs.110).aspx



建议是,如果试图删除用户不是当前登录然后必须在if条件检查中使用Roles.IsUserInRole(UserName,Provisional),否则条件将为true并将尝试删除没有该角色的用户。

I am getting the following error in asp.net application
The user 'a' is already not in role 'Provisional'

the relevant portion of code where I get this error is (line 29 below)

Line 27: if(Roles.IsUserInRole("Provisional"))
Line 28: {
Line 29: Roles.RemoveUserFromRole(username, "Provisional");
Line 30: Roles.AddUserToRole(username, "Provisional1");
Line 31: Response.Redirect("~/Profiles/ProfileBasics.aspx?");
Line 32 }

Since to user is not in role Provisional, this part of code should have been overlooked by server.

I want to perform this code only if User is in role Provisional, else other code works. But I invariably get this error when User is in role Provisional.

Kindly help me overcome this error.

I am new to asp.net and I am using C#

Many thanks for help

解决方案

Without the rest of your code it's impossible to give you the exact answer, but...
Your if test checks if the current user is in a role, and if he is continues with the Remove: Roles.IsUserInRole[^]
The remove uses the username that you pass to the method: Roles.RemoveUserFromRole[^], not the current user.

So, if the current logged in user (i.e. you) is in the Provisional role, then you will try to remove whoever has the username in your username variable from Provisional.

I'm guessing that the current logged in user is not the same as the user in username...use the debugger and check!


As OriginalGriff explained in the solution ,
Roles.IsUserInRole("Provisional") => checks with current logged in User
http://msdn.microsoft.com/en-us/library/4z6b5d42(v=vs.110).aspx

Roles.IsUserInRole("UserName", "Provisional")=>checks with the specified user is in that Role.

http://msdn.microsoft.com/en-us/library/bz1zy88e(v=vs.110).aspx

Suggestion is ,if trying to remove a user who is not the current logged in then have to use Roles.IsUserInRole("UserName", "Provisional") in your if condition check or else the condition will be true and will try to remove a user who is not having the role.


这篇关于如果声明没有给出理想的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆