首页 > 解决方案 > ASP.Net 在“UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);”上出现错误

问题描述

我正在尝试部署一个 ASP.net webapp - 我最初运行的是以下代码:

   PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    UserPrincipal user = UserPrincipal.Current;

    string displayName = user.DisplayName;
    string username = user.SamAccountName; 

但我收到以下错误:

无法将“System.DirectoryServices.AccountManagement.GroupPrincipal”类型的对象转换为“System.DirectoryServices.AccountManagement.UserPrincipal”类型。

正如其他帖子中所建议的那样,在 IIS 中,我将授权更改为 Windows 授权并将我的代码更改为:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);

但是,Identity 一词带有红色下划线,错误显示为:

“SqlDataSource”不包含“Identity”的定义,并且找不到接受“SqlDataSource”类型的第一个参数的可访问扩展方法“Identity”(您是否缺少 using 指令或程序集引用?)

有谁知道我该如何解决这个问题?

完整的cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Data;
using System.IO;

namespace Assets
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {



            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);


            if ((dgUser.Rows.Count == 0) || (dgUser.Rows[0].Cells[3].Text == "0"))
            {

                Response.Redirect("AccessDenied.aspx");
            }
            else
            {
                string admin = dgUser.Rows[0].Cells[4].Text;
                if (admin.ToString() == "1")
                {
                    sidebarConfig.Visible = true;
                }
                else
                {
                    sidebarConfig.Visible = false;
                }
            }

        }

        protected void gvUser_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void users_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }
    }
}

完整的 aspx 代码是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Assets.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
    <title>
    </title>
    <link href="stylesheet.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            margin-bottom: 4px;
        }
        .auto-style3 {
            font-family: "Segoe UI";
            font-size: small;
        }
        .auto-style4 {
            font-family: "Segoe UI";
            font-size: small;
            color: #C84C09;
        }
        .auto-style5 {
            height: 120px;
            width: 120px;
            float: left;
        }
    </style>
</head>
<body>
<form id="form1" runat="server">

<div class="container">

<%--Header--%>  
    <div class="header" style="height:120px; margin-right: 15px; background-color:#3B3B3B;">
      <div class="logo">
        <img alt="Logo" src="Images/logo%20white%20only.jpg" class="auto-style5" />
          <asp:Label ID="lbUser" runat="server" Text="Label"></asp:Label>
          <br />
          <asp:SqlDataSource ID="User" runat="server" ConnectionString="<%$ ConnectionStrings:csAssets %>" SelectCommand="SELECT DISTINCT * FROM [USERS] WHERE ([USERNAME] = @USERNAME)">
              <SelectParameters>
                  <asp:ControlParameter ControlID="lbUser" Name="USERNAME" PropertyName="Text" Type="String" />
              </SelectParameters>
          </asp:SqlDataSource>
          <br />
          Label</div>
        <%--<img alt="HeaderText" style="height: 110px; "  src="Images/HeaderText4.jpeg" />--%>
    </div>

<div class="body" style="margin-top: 90px;">

<%--Sidebar--%>  
    <div class="sidebar">
        <br />
        <a href ="Default.aspx" class="active" />
            <table style="width:100%;">
                <tr>
                    <td class="sidebarIcons" >
                        <asp:Image ID="Image2" runat="server" Height="25px" Width="25px"  ImageUrl="~/Images/home.png" />
                    </td>
                    <td class="sidebarText">
                        &nbsp;&nbsp;Home
                    </td>
                </tr>
            </table>

        <a href="AddNew.aspx" >
            <table style="width:100%;">
                <tr>
                    <td class="sidebarIcons">
                        <asp:Image ID="Image5" runat="server" Height="25px" Width="25px"  ImageUrl="~/Images/edit.png" />
                    </td>
                    <td class="sidebarText">
                        &nbsp;&nbsp;New Record
                    </td>
                </tr>
            </table>
        </a>
        <a href="Search.aspx" >
            <table style="width:100%;">
                <tr>
                    <td class="sidebarIcons">
                        <asp:Image ID="Image6" runat="server" Height="25px" Width="25px"  ImageUrl="~/Images/case.png" />
                    </td>
                    <td class="sidebarText">
                        &nbsp;&nbsp;View Record
                    </td>
                </tr>
                </table>
        </a>
        <a href="BulkReview.aspx">
            <table class="auto-style1">
                <tr>
                    <td class="sidebarIcons">
                        <asp:Image ID="Image3" runat="server" Height="25px" Width="25px"  ImageUrl="~/images/accept.png" />
                    </td>
                    <td class="sidebarText">
                        &nbsp;&nbsp;Bulk Review
                    </td>
                </tr>
            </table>
        </a>
        <a href="Configurations.aspx" id="sidebarConfig" runat="server">
            <table id="taConfig" class="auto-style1">
                <tr>
                    <td class="sidebarIcons">
                        <asp:Image ID="Image7" runat="server" Height="25px" Width="25px"  ImageUrl="~/Images/config.png" />
                    </td>
                    <td class="sidebarText">
                        &nbsp;&nbsp;Configuration
                    </td>
                </tr>
            </table>
        </a>
    </div>

    <div class="content" >
        <br />
        <span class="auto-style3">The Assets and Liabilities App split records into three parts:&nbsp; The Header record, Documents and Attributes.&nbsp; Each header Record can have multiple Documents and Attributes. Attributes can also reference saved documents.&nbsp; the field that are availble on the Header Record are dependant on the Asset Type selected.&nbsp; The only required fields for all Asset Types are:&nbsp; Asset Type, Service Owner and Description.</span><br class="auto-style3" />
        <br class="auto-style3" />
        <span class="auto-style4"><strong>New Record</strong></span><br class="auto-style3" />
        <span class="auto-style3">Add a new header Record and the click &#39;Next&#39;, you can then upload multiple documents and add multiple attributes.</span><br class="auto-style3" />
        <br class="auto-style3" />
        <span class="auto-style4"><strong>View Record</strong></span><br class="auto-style3" />
        <span class="auto-style3">From the &#39;My Teams Records&#39; drop down menu, you can select records where the service owner is your team.&nbsp; To find other records, click on &#39;Advanced Search&#39; and search by any of the available fields and click &#39;Search&#39;.&nbsp; this will produce a list of records that meet your search criteria, find the records that you&#39;re after and click View. </span>
        <br class="auto-style3" />
        <br class="auto-style3" />
        <span class="auto-style3">To edit a record click on the Edit icon on the header section, document section or attribute selection, make the amendment and click Save.&nbsp; When Editing the header section you can also review the record, this will change the review date to today&#39;s date.&nbsp; You can also delete the record from here. To add a new Document or Attribute you can click on the relevant Edit icon and then click Add Document or Add Attribute.</span><br class="auto-style3" />
        <br class="auto-style3" />
        <span class="auto-style4"><strong>Bulk Review</strong></span><br class="auto-style3" />
        <span class="auto-style3">This enables you to review multiple records at once. You can either select all of your teams records, or just records that are due for review within the next month or records that are past their review date.&nbsp; By reviewing records you will change the review date to today&#39;s date.&nbsp; You can set how often a record should be reviewed by editing the Review Period field in the header record.</span><br class="auto-style3" />
        <br class="auto-style3" />
        <span class="auto-style4"><strong>Configuration</strong></span><br class="auto-style3" />
        <span class="auto-style3">If you are set up as an Administrator on the app, you can access the Configuration settings.&nbsp; This enables you to add new users, inactivate users, grant Admin privilages or add/edit:&nbsp; Asset types, Contract types, Attrinute Types and Document Types.&nbsp; If you think you should be an Administrator, but can&#39;t see the Configuration option on the sidebar menu, please contact an existing Administrator, or a member of the Business Intelligence Team.</span><br class="auto-style3" />
        <br />
        <br />
        <asp:GridView ID="Records" runat="server">
        </asp:GridView>
        <br />

        <asp:GridView ID="dgUser" runat="server" AutoGenerateColumns="False" CssClass="auto-style2" DataSourceID="userstable" Visible="False" >
            <Columns>
                <asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
                <asp:BoundField DataField="USERNAME" HeaderText="USERNAME" SortExpression="USERNAME" />
                <asp:BoundField DataField="SERVICE_AREA" HeaderText="SERVICE_AREA" SortExpression="SERVICE_AREA" />
                <asp:BoundField DataField="ACTIVE" HeaderText="ACTIVE" ReadOnly="True" SortExpression="ACTIVE" />
                <asp:BoundField DataField="ADMIN" HeaderText="ADMIN" ReadOnly="True" SortExpression="ADMIN" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="userstable" runat="server" ConnectionString="<%$ ConnectionStrings:csAssets %>" SelectCommand="SELECT * FROM [vwUSERS] WHERE ([USERNAME] = @USERNAME)">
            <SelectParameters>
                <asp:ControlParameter ControlID="lbUser" Name="USERNAME" PropertyName="Text" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
          <p>
              &nbsp;</p>
    </div>
  </div>
  <div class="footer"></div>
</div>
</form>
</body>
</html>

任何帮助将不胜感激

非常感谢

标签: c#asp.netdirectoryservices

解决方案


推荐阅读