简体   繁体   English

如何使用asp.net/C#在网页上显示Windows身份验证?

[英]How to display Windows Authentication on webpage with asp.net/C#?

enter image description here I'm new to programming so any help would be much appreciated on that note. 在这里输入图像描述我是编程的新手,因此对该注释的任何帮助将不胜感激。 Here is my issue I'm using windows authentication to validate users and I want to display that users name on my web page. 这是我使用Windows身份验证来验证用户的问题,并且我想在我的网页上显示该用户名。 EX: "Hi Timmy" whenever they come to my web page. 例如:每当他们来到我的网页时,“嗨,蒂米”。

I added system.directoryservices.accountmanagement, but honestly not sure if that is the right path if i'm just not using it correctly with UserPrincipal.DisplayName. 我添加了system.directoryservices.accountmanagement,但是老实说,如果我只是没有正确地使用UserPrincipal.DisplayName,则不确定这是否是正确的路径。 Committed out is just some other ideas that I have tried. 承诺只是我尝试过的其他一些想法。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Person_Search.aspx.cs" Inherits="Workwithme.WebPages.Person_Search" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Person Search</title>
    <link rel="stylesheet" type="text/css" href="/Styles/StyleSheet.css" />
</head>
<body>
    <header>
        <img src="/Images/NGSC.gif" style="max-width: 100%; height: 250px; background-repeat: no-repeat; margin: auto; display: block;" />

            <h1 style="text-align:center;color:white; width: 100%;">Search For A Person <br /> Phone Directory</h1>
        </header>
          <div id="navigation" style="text-align:center;">
                    <a href="Person_Search.aspx">Person Search</a>  
                    <a href="Person_Update.aspx">Personal Update</a>
            </div>
    <br />
    <div>
        <form id="form1" runat="server" style="background-color:#112e51; color:white;" >     
            <br />
            <h3>UserPrincipal.DisplayName</h3>

            <%--<asp:LoginName id="LoginName1" runat="server" 
               FormatString ="Welcome,{0}" />
            <h3>{{User.DisplayName}}</h3> --%>  

For windows authentication make sure your IIS is properly set up .It should show windows authentication dialog in browsers(except IE where integrated windows authentication maybe on ). 对于Windows身份验证,请确保您的IIS设置正确。它应在浏览器中显示Windows身份验证对话框(IE除外,其中可能启用了集成Windows身份验证)。 Now Since you have 现在,既然你有

Create an ASP.NET Web Application named ASPNETWinAuth. 创建一个名为ASPNETWinAuth的ASP.NET Web应用程序。 By default, theWebForm1.aspx file appears. 默认情况下,将显示WebForm1.aspx文件。 In the HTML view of WebForm1.aspx, replace the existing code with the following sample code: 在WebForm1.aspx的HTML视图中,将现有代码替换为以下示例代码:

<%=User.Identity.Name%>

Click Start, point to Programs, point to Administrative tools, and then click Internet Information Services. 单击开始,指向程序,指向管理工具,然后单击Internet信息服务。 The Internet Information Services MMC appears. Internet信息服务MMC出现。 Expand Computer, and then expand a Web site that uses Windows authentication. 展开“计算机”,然后展开一个使用Windows身份验证的网站。 Click the ASPNETWinAuth Web site application. 单击ASPNETWinAuth网站应用程序。 On the Action menu, click Properties. 在“操作”菜单上,单击“属性”。 In Properties, click the Directory Security tab. 在“属性”中,单击“目录安全性”选项卡。 Under Anonymous access and authentication control, click Edit. 在“匿名访问和身份验证控制”下,单击“编辑”。 In Authentication Methods, click to select Integrated Windows authentication. 在“身份验证方法”中,单击以选择“集成Windows身份验证”。 Click to clear all other check boxes. 单击以清除所有其他复选框。

After you configure the IIS Web site for Integrated Windows Authentication, you must configure the ASP.NET application to recognize authenticated users. 在为集成Windows身份验证配置IIS网站之后,必须配置ASP.NET应用程序以识别经过身份验证的用户。 To do this, you must change the Web.config file. 为此,您必须更改Web.config文件。 In the Web.config file, locate the tag, and then set the mode attribute to Windows, as in the following example: Click OK. 在Web.config文件中,找到标签,然后将mode属性设置为Windows,如以下示例所示:单击“确定”。 In Properties, click OK. 在属性中,单击确定。 The ASPNETWinAuth Web application is now configured to accept valid user accounts. 现在已将ASPNETWinAuth Web应用程序配置为接受有效的用户帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM