asp.net - 在 webMethod 上使用 FormsAuthentication

标签 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).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/

相关文章:

html - 从AJAX向用户显示错误时出现问题

javascript - 从 API 获取 JSON 子集以在 Web 上显示

php - 将数据从 PHP 脚本发送回 JQuery AJAX 请求

c# - 仅使用 JavaScript 在 ASP.NET 中滑动(或移动)控件

c# - 如何确保在按钮的单击事件之前触发文本框的 textchanged 事件?

c# - 数据表中的验证

c# - Odata V4 DateTimeOffset 过滤器问题

javascript - 使用自定义请求 header 打开 URL

javascript - Jquery切换类问题

javascript - 使用 Mustache 遍历对象列表