Jorge Hernández
Jorge Hernández

Reputation: 58

how to get AspNet Identity User ID directly in .aspx?

I´m developing a Web Forms application using VS 2013. In "MyReports.aspx" I want the user to see only the reports that were issued by him/her. In the SqlDataSource SelectParameters I defined the UserID Parameter and then pass the UserId in the codebehind. But, How can I do it directly in the aspx?

I´ve tried adding the Microsoft.AspNet.Identity namespace trough

<%@ Import Namespace="Microsoft.AspNet.Identity" %>

And then setting the parameter like:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ReportsByIssuingUserView] WHERE ([UserID]=@UserID)">
    <SelectParameters>
        <asp:Parameter Name="UserID" Type="String" DefaultValue="<%: Context.User.Identity.GetUserId() %>"/>
    </SelectParameters>
</asp:SqlDataSource>

With no luck...

My code right now is the following and works great:

ASPX:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ReportsByIssuingUserView] WHERE ([UserID]=@UserID)">
    <SelectParameters>
        <asp:Parameter Name="UserID" Type="String"/>
    </SelectParameters>
</asp:SqlDataSource>

CS:

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.SelectParameters["UserID"].DefaultValue = Context.User.Identity.GetUserId();
    }

Is there any way to do it without using the codebehind?

Thanks!

Upvotes: 2

Views: 1626

Answers (1)

Cam Bruce
Cam Bruce

Reputation: 5689

Use the <%= Context.User.Identity.GetUserId() %> expression syntax inline in your ASPX page.

Upvotes: 1

Related Questions