如何获取当前用户ID

时间:2015-07-28 20:02:49

标签: asp.net

我正在开发一个网页,当您注册并登录时,您需要填写您的信息(姓名,姓名,职业,性别)。 我创建了一个存储过程:

ALTER PROCEDURE [dbo].[añadirinfocontacto]
    @id int,@apellidoP nvarchar(50),@apellidoM nvarchar(50),@nombre nvarchar(50),@idEspecialidad int,@sexo nvarchar(10)
AS
BEGIN
    if(@id=1)
    begin
    update Tbl_alumnos set nombre=@nombre,apellido_paterno=@apellidoP,apellido_materno=@apellidoM,id_especialidad=@idEspecialidad,sexo=@sexo where id_usuarios=@id
    end
    else
    if(@id=2)
    begin
    update Tbl_Maestros set nombre=@nombre,apellido_paterno=@apellidoP,apellido_materno=@apellidoM,id_especialidad=@idEspecialidad,sexo=@sexo where id_usuarios=@id
    end
    else
    begin
    update Tbl_Administradores set nombre=@nombre,apellido_paterno=@apellidoP,apellido_materno=@apellidoM,id_especialidad=@idEspecialidad,sexo=@sexo where id_usuarios=@id
    end

END

代码:

public static EntUsuario Actualizar(int idU,string nombre, string Apellidop,string ApellidoM,int IdEsp,string sexo)
{
    EntUsuario obj = null;
    SqlCommand cmd = null;
    SqlDataReader dr = null;
    try
    {
        Conexion cn = new Conexion( );
        SqlConnection cnx = cn.conectar();
        cmd = new SqlCommand("añadirinfocontacto", cnx);
        cmd.Parameters.AddWithValue("@id", idU);
        cmd.Parameters.AddWithValue("@nombre", nombre);
        cmd.Parameters.AddWithValue("@apellidoP", Apellidop);
        cmd.Parameters.AddWithValue("@apellidoM", ApellidoM);
        cmd.Parameters.AddWithValue("@idEspecialidad", IdEsp);
        cmd.Parameters.AddWithValue("@sexo", sexo);
        cmd.CommandType = CommandType.StoredProcedure;

        cnx.Open();
        cmd.ExecuteNonQuery();
        cnx.Close();


    }
    catch (Exception e)
    {
        obj = null;

    }
    finally
    {
        cmd.Connection.Close();
    }
    return obj;

}

Cick按钮事件

protected void btnConfirmar_Click(object sender, EventArgs e)
        {
            Guid guid = (Guid)Membership.GetUser().ProviderUserKey;
            EntUsuario obj = GbUsuario.Actualizar(Convert.ToInt32(guid),txtNombre.Text,txtApP.Text,txtApM.Text,Convert.ToInt32(dpEspecialidad.SelectedValue),RadioButtonList1.SelectedValue);

        }

我想获得de用户的ID,但我不知道该怎么做,我读过Session [“user_id”],Membership.getUser,我还读到我需要配置web.config。

如果我使用上面的示例代码,我使用Membership.getUser但出现错误,说它无法连接到数据库

1 个答案:

答案 0 :(得分:0)

if os.path.isfile('some_chars.txt') or datetime.now()<datetime.strptime('30-8-2015','%d-%m-%Y'): # DO WHAT application HAS TO DO else: print 'TRIAL EXPIRED' quit() You can get the current user with the following code:

enter code here

You can force users to log in by adding this to the web.config:

var user = System.Web.HttpContext.Current.User
相关问题