简体   繁体   中英

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. 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. 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 ). Now Since you have

Create an ASP.NET Web Application named ASPNETWinAuth. By default, theWebForm1.aspx file appears. In the HTML view of WebForm1.aspx, replace the existing code with the following sample code:

<%=User.Identity.Name%>

Click Start, point to Programs, point to Administrative tools, and then click Internet Information Services. The Internet Information Services MMC appears. Expand Computer, and then expand a Web site that uses Windows authentication. Click the ASPNETWinAuth Web site application. 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. 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. To do this, you must change the Web.config file. In the Web.config file, locate the tag, and then set the mode attribute to Windows, as in the following example: Click OK. In Properties, click OK. The ASPNETWinAuth Web application is now configured to accept valid user accounts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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