Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在webMethod上使用FormsAuthentication_Asp.net_Ajax_Jquery_Webmethod - Fatal编程技术网

Asp.net 在webMethod上使用FormsAuthentication

Asp.net 在webMethod上使用FormsAuthentication,asp.net,ajax,jquery,webmethod,Asp.net,Ajax,Jquery,Webmethod,我有一个页面有一个评论部分。本节与WebMethod通信,以插入新注释 [WebMethod] public static bool insertComment(string commentString) { //userName validation here string userName = (FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Nam

我有一个页面有一个评论部分。本节与
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
发送信息,但是,该信息字段可能很容易更改。 那个么,在服务器端,哪种方法可以用来知道哪个用户在发帖呢?
非常感谢

请求
对象是驻留在
页面
中的实例,因此您需要引用才能在静态上下文中访问此对象。您可以使用
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);
}

Request
是需要引用的对象吗?是!很抱歉。。。这是唯一需要参考的部分。太好了!我要试试看。谢谢兄弟!