我有一个包含评论部分的页面。此部分与 WebMethod
通信以插入新注释。
[WebMethod]
public static bool insertComment(string commentString)
{
//userName validation here
string userName = (FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name);
return new CommentClass().InsertComment(commentString, userName);
}
问题是:“非静态字段需要对象引用”。
我知道我可以从隐藏字段或 div
发送信息,但是,该信息字段可以轻松更改。
那么在服务器端可以使用哪种方式来知道哪个用户正在发帖呢?
非常感谢!
最佳答案
Request
对象是一个位于 Page
中的实例,因此您需要一个引用来在静态上下文中访问该对象。您可以使用 HttpContext.Current.Request
访问此上下文中的 Request
。
[WebMethod]
public static bool insertComment(string commentString)
{
//userName validation here
string userName =
(FormsAuthentication.Decrypt(
HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name);
return new CommentClass().InsertComment(commentString, userName);
}
关于asp.net - 在 webMethod 上使用 FormsAuthentication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15532704/