我正在尝试使用名为“Member”的Web服务类。 localhost是Web服务“Authenticate”的Web引用。我正在使用ASP.NET中的登录控件创建登录页面。这段代码在登录例程中。你能给我一个提示吗?我希望登录控件读取webservice返回的字符串,以便我可以检查用户是否经过身份验证。如果它返回“noaccess”,则该人无法访问受保护的页面。谢谢:))
protected void lgnMemeber_Authenticate(object sender, AuthenticateEventArgs e)
{
try {
string emailAddress = lgnMemeber.UserName;
string userPassword = lgnMemeber.Password;
localhost.Member memb = new localhost.Member();
localhost.Authenticate auth = new localhost.Authenticate();
string authReturn=auth.Authenticatee(emailAddress, userPassword).ToString();
lblError.Text = emailAddress; lblError.Text += userPassword; lblError.Text += authReturn;
//string access = "noaccess";
// if (authReturn.Contains(access))
//{
// e.Authenticated = false;
//}
//else
//{
// e.Authenticated = true;
//}
}
catch(Exception ex) {
lblError.Text += ex.Message;
}
}
这是我的webService:
/// <summary>
/// Member class
/// </summary>
public class Member
{
public string accessLevel;
/// <summary>
/// Member constructor
/// </summary>
public Member()
{
accessLevel = "noaccess";
}
}
/// <summary>
/// HASC Authentication Web Service
/// </summary>
[WebService(Description = "Hamilton Adult Soccer Club (HASC) Authentication Web Service.", Namespace = "http://mohawkcollege.ca/hasc")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Authenticate : System.Web.Services.WebService {
public Authenticate () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
/// <summary>
/// Take the email and password of the user. If user exists, it returns the access level and the PesronID of that member
/// </summary>
/// <param name="Email">Email of the member</param>
/// <param name="Password">Password of the member to authenticate</param>
/// <returns>Access level and PersonID of authenticated member</returns>
[WebMethod(Description = "<ul><li>Accepts 2 string parameters, Email and Password.</li><li>Returns a string indicating access level and the PersonID of the authenticated club member</li></ul>")]
public Member Authenticatee(string Email, string Password)
{
Member member = new Member();
string con_string = WebConfigurationManager.ConnectionStrings["HASCConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(con_string);
SqlCommand cmd = new SqlCommand("SELECT PersonID, Email, UserPassword, Player, Coach, Referee, Administrator FROM Persons WHERE Email='" + Email+"' AND UserPassword='"+Password+"'", con);
try
{
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
int personID = reader.GetInt32(0);
bool player = reader.GetBoolean(3);
bool coach = reader.GetBoolean(4);
bool referee =reader.GetBoolean(5);
bool admin = reader.GetBoolean(6);
if (Email == reader.GetString(1) && Password==reader.GetString(2))
{
if (player) {
member.accessLevel = "player,"+personID;
}
else if(coach) {
member.accessLevel = "coach,"+personID;
}
else if (referee)
{
member.accessLevel = "referee,"+personID;
}
else if (admin)
{
member.accessLevel = "admin,"+personID;
}
else
{
member.accessLevel = "accessapproved";
}
}
}
}
}
catch
{
member.accessLevel = "noaccess";
}
return member;
}
}
答案 0 :(得分:3)
更改此行:
string authReturn=auth.Authenticatee(emailAddress, userPassword).ToString();
对此:
string authReturn = auth.Authenticatee(emailAddress, userPassword).accessLevel;
或...覆盖您的Member类中的ToString方法:
public class Member
{
...
public override string ToString()
{
return accessLevel;
}
}
顺便说一下,建议不要使用公共字段(如accessLevel):将其设为私有,或改为使用公共属性:Why won't anyone accept public fields in C#?