【问题标题】:Issue with WebMethod being Static in C# ASP.NET CodebehindWebMethod 在 C# ASP.NET 代码隐藏中是静态的问题
【发布时间】:2017-01-18 20:54:41
【问题描述】:

由于单个页面上有多个表单导致的问题,我使用 AJAX 调用 WebMethod 来提交我的表单,而不是使用 ASP 控件。但是,在执行此操作时,我以前用于在数据库中创建新条目的方法不再有效,因为 WebMethod 必须是静态的。

我已经使用 ASPX 身份验证对我的用户进行了身份验证,并且正在尝试使用代码隐藏检索该用户的用户名和 ID。用户已在 Page_Load 上通过身份验证,但似乎我无法通过我的 WebMethod 访问此信息。这可以在静态 WebMethod 内部进行吗?提前感谢您的所有帮助!

[WebMethod]
public static void CreateJob()
{
    Submit_Job();
}

public static void Submit_Job()
{
    if (Page.User.Identity.IsAuthenticated)
    {
        try
        {
            string username = Context.User.Identity.Name;
        }
        catch
        {
            Context.GetOwinContext().Authentication.SignOut();
        }
    }

    var manager = new UserManager();
    var usernameDatabase = new ApplicationUser() { UserName = username };
    usernameDatabase = manager.Find(username, "password here");
    if (usernameDatabase != null)
    {
        IdentityHelper.SignIn(manager, usernameDatabase, isPersistent: false);  

        string jobTitle = Request.Form["jobTitle"];

        using (var ctx = new CreateUserContext(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString))
        {
            Job job = new Job()
            {
                job_title = jobTitle
            };

            ctx.Jobs.Add(job);
            ctx.SaveChanges();
        }
    }
}

编辑: 例如,Page.User.Identity.IsAuthenticated 存在错误——页面、上下文和请求都显示它们不能是静态的。

具体错误: (非静态字段、方法或属性“Control.Page”需要对象引用)以及上下文和请求。

【问题讨论】:

  • 具体是哪里出了问题,错误信息是什么?
  • @arbitrarystringofletters 抱歉,我刚刚添加了包含更多信息的错误消息
  • 我最近遇到了同样的问题。幸运的是,每当用户登录应用程序时,我们都会将用户信息加密存储到会话变量中,因此我检索该信息,将其传递给用户的类构造函数,后者将其解密,我可以轻松使用我的用户登录信息.因此,我的解决方案是将用户信息存储在 Session 中,但要小心存储的内容。也许序列化用户对象并存储在会话中,然后在需要时反序列化它
  • 这个question 可能会有所帮助。尝试将<modules runAllManagedModulesForAllRequests="true" /> 添加到您的配置文件中。
  • @CJLopez 我从没想过,谢谢!有没有可以链接到的地方可以显示如何做到这一点?我以前没有使用过会话变量。

标签: c# asp.net entity-framework webmethod


【解决方案1】:

从一个简单的评论移走它

我最近遇到了同样的问题。

幸运的是,每当用户登录我们的应用程序时,我们都会将用户信息加密存储到会话变量中,因此我会检索该信息,将其传递给用户的类构造函数,后者会对其进行解密,然后我可以使用我登录的用户信息没有麻烦。

所以,我的解决方案是将用户信息存储在 Session 中,但要小心存储的内容。也许序列化用户对象并存储在会话中,然后,在你需要的时候

public void Page_Load()
{
    // Retrieve authenticated user information
    UserClass userObject = GetUserCredentials();
    // Call a method that turns the authenticated user object into a string that contains the users session information. Given the sensivity of this information, might want to try to encrypt it or offuscate it. Store it in a session variable as a string
    Session["UserContext"] = userObject.SerializeUser()
    /* rest of the page code goes here */
}

[WebMethod(EnableSession=true)]
public static void CreateJob()
{
    Submit_Job();
}

public static void Submit_Job()
{
    // Lets get the authenticated user information through the session variable. Due to the static nature of the method, we can't access the Session variables directly, so we call it using the current HttpContext
    string serializedUserInfo = )HttpContext.Current.Session["UserContext"].ToString();

   // Let's create the users object. In my case, we have a overcharged constructor that receives the users serialized/encrypted information, descrypts it, deserializes it, and return a instance of the class with the deserialized information
   UserClass userObject = new UserClass(serializedUserInfo);
   // Do whatever the method has to do now!
}

关于序列化的主题,用“c# object serialization”快速谷歌搜索会为你带来几个很好的匹配。 XML 和 JSON 是最常用的两种序列化,特别是在 Web 方法上。二进制序列化也是混淆登录用户信息的好选择

【讨论】:

  • 非常感谢!这正是我所需要的。非常感谢您的帮助
  • 很高兴听到这个消息
猜你喜欢
  • 1970-01-01
  • 2020-02-11
  • 2018-02-16
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多