Webform到SQL数据库 - 如何传递user.identity.name?

问题描述 投票:0回答:1

我有一个构建良好的webform,写回我的SQL数据库,但现在我需要跟踪进行更改的人的用户ID。我是一名SQL开发人员,所以我的知识范围有点偏离。

我的.aspx文件有

<InsertParameters>
    .....
    <asp:Parameter Name="StaffId" Type="String" DefaultValue= "Anonymous"/>

我的.aspx.cs文件如下所示:

public partial class _BLAHBLAHBLAH_Topic1 : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["UserPermission"] = null;                
            string username = User.Identity.Name;                

            if (username.StartsWith("ABC\\"))
                username = username.Remove(0, 4);

            bool[] userPermssion = GetUserPermissions(username);

            if(!userPermssion[0])
            {
                ASPxGridView1.Visible = false;
                WarningLabel.Visible = true;
            }                
        }
    }

    private bool[] GetUserPermissions(string username)
    {
        bool canView = false;
        bool canUpdate = false;
        bool canDelete = false;
        bool canInsert = false;

        try
        {
            PermissionDataSet.UserPermissionsDataTable userDataTable = new PermissionDataSet.UserPermissionsDataTable();
            PermissionDataSetTableAdapters.UserPermissionsTableAdapter adapter = new PermissionDataSetTableAdapters.UserPermissionsTableAdapter();
            adapter.Fill(userDataTable, username);

            if (userDataTable != null)
            {
                if (userDataTable.Rows.Count == 1)
                {
                    canView = Convert.ToBoolean(userDataTable.Rows[0]["ViewFlag"]);
                    canUpdate = Convert.ToBoolean(userDataTable.Rows[0]["UpdateFlag"]);
                    canDelete = Convert.ToBoolean(userDataTable.Rows[0]["DeleteFlag"]);
                    canInsert = Convert.ToBoolean(userDataTable.Rows[0]["InsertFlag"]);
                }
            }
        }
        catch(Exception ex)
        {
            //unable to retrieve permissions - all values are defaulted to false
        }

        bool[] userPermission = new bool[] { canView, canUpdate, canDelete, canInsert };
        Session["UserPermission"] = userPermission;

        return userPermission;
    }

    protected void ASPxGridView1_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
    {
        if (Session["UserPermission"] != null)
        {
            bool[] permission = (bool[])Session["UserPermission"];

            switch (e.ButtonType)
            {
                case ColumnCommandButtonType.Edit:
                    e.Visible = permission[1];
                    break;

                case ColumnCommandButtonType.Delete:
                    e.Visible = permission[2];
                    break;

                case ColumnCommandButtonType.New:
                    e.Visible = permission[3];
                    break;
            }                      
        }
        else
        {
            switch (e.ButtonType)
            {
                case ColumnCommandButtonType.Edit:
                    e.Visible = false;
                    break;

                case ColumnCommandButtonType.Delete:
                    e.Visible = false;
                    break;

                case ColumnCommandButtonType.New:
                    e.Visible = false;
                    break;
            }
        }
    }
}

我想我需要放一个

protected void Page_Init(object sender, EventArgs e)
{
    DataSource.SelectParameters["StaffId"].DefaultValue = User.Identity.Name;
}

那里的代码片段,但我真的不知道在哪里或如何,所以任何建议都会非常感激。

谢谢

sql asp.net parameter-passing
1个回答
1
投票

使用@done_merson在How to use User.Identity.Name as a parameter for SqlDataSource in ASP.NET?上的建议完成了这个

有魅力!谢谢@done_merson

© www.soinside.com 2019 - 2024. All rights reserved.