You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#音乐APP开发:Supabase登录遇CS1061 Session属性不存在错误

Supabase C# 登录认证出现 CS1061 错误

错误信息

CS1061: 'Session' does not contain a definition for 'Session', and no accessible 'Session' extension method accepting a first argument of type 'Session' could be found.

问题代码

using System;
using System.Windows.Forms;
using Supabase;
using Supabase.Gotrue;

namespace Musicapp1._0
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            var client = new Supabase.Client("https://my-supabase-url.supabase.co", "my-anon-key");

            try
            {
                var signInResponse = await client.Auth.SignInWithPassword(textBoxUser.Text, textBoxPw.Text);

                // 错误发生在此处:
                if (signInResponse.User != null && signInResponse.Session != null)
                {
                    MessageBox.Show("Login successful");
                }
                else
                {
                    MessageBox.Show("Login failed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Login error: " + ex.Message);
            }
        }
    }
}

已尝试的解决方法

  • 更新相关包:
    dotnet add package Supabase
    dotnet add package Supabase.Gotrue
    
  • 添加命名空间 using Supabase.Gotrue;

问题原因及解决方案

错误核心是Supabase C# SDK版本变更导致的API差异:新版本的SignInWithPassword方法不再返回包含Session属性的响应对象,而是直接返回Session类型实例——你当前拿到的signInResponse本身就是一个Session对象,不需要再访问它的Session属性。

修正后的代码如下:

private async void button1_Click(object sender, EventArgs e)
{
    var client = new Supabase.Client("https://my-supabase-url.supabase.co", "my-anon-key");

    try
    {
        // 返回值直接是Session实例
        var session = await client.Auth.SignInWithPassword(textBoxUser.Text, textBoxPw.Text);

        if (session != null && session.User != null)
        {
            MessageBox.Show("Login successful");
        }
        else
        {
            MessageBox.Show("Login failed");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Login error: " + ex.Message);
    }
}

额外注意事项:

  1. 确保使用Supabase SDK的最新稳定版,不同版本的API差异较大
  2. 初始化Supabase Client时建议全局单例使用,避免重复创建实例

内容的提问来源于stack exchange,提问作者Ren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:23:09