2017-07-19 38 views
0

我正在使用ASP.NET MVC 4中的混合身份驗證模式,並且我希望Windows用戶名可以自動填充到我的文本框中。我已經使用System.Security.Principal.WindowsIdentity.GetCurrent().Name;,它在本地工作正常,但是當我在服務器上執行它時,「Default AppPool」將進入用戶名文本框。MVC 4自動填充窗口用戶名

我CSHTML碼 -

@model TMVCRepository.Models.ActivedirectoryModels 
    @{ 
    ViewBag.Title = "Active directory authentication"; 

    } 
@{ 

    string UserIDwindows = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

    string[] Usernameis = UserIDwindows.Split('\\'); 


    if (ViewData["Error"] != "" && ViewData["Error"] != null) 
    { 
     string res = ViewData["Error"].ToString(); 

    <span style="color: red; font-size: 14px;"> 

     @res 
    </span> 

     ViewData["Error"] = null; 



    } 


} 

@using (Html.BeginForm("index", "Activedirectory")) 
{ 


    <h2>ENTER YOUR NETWORK/SYSTEM LOGIN CREDENTIALS 


    </h2> 


    <table width="100%"> 

     <tr> 

      <td> 

       @Html.LabelFor(a => a.UserID) 

      </td> 

     </tr> 

     <tr> 

      <td> 

       @Html.TextBoxFor(a => a.UserID, new { @Value = Usernameis[1] }) 
       @* @Html.TextBox(UserID,)*@ 

       @Html.ValidationMessageFor(a => a.UserID) 

      </td> 

     </tr> 

     <tr> 

      <td> 

       @Html.LabelFor(a => a.Password) 

      </td> 

     </tr> 

     <tr> 

      <td> 

       @Html.TextBoxFor(a => a.Password) 

       @Html.ValidationMessageFor(a => a.Password) 

      </td> 

     </tr> 

     <tr> 

      <td> 

       @Html.LabelFor(a => a.DomainName) 

      </td> 

     </tr> 

     <tr> 

      <td> 

       @Html.TextBoxFor(a => a.DomainName) 

       @Html.ValidationMessageFor(a => a.DomainName) 

      </td> 

     </tr> 


     <tr> 

      <td colspan="2"> 

       <input id="Submit1" type="submit" value="submit" /> 



      </td> 

     </tr> 

    </table> 

} 

    my controller. 

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Mvc; 
    using TMVCRepository.Models; 
    using Telerik.Web.Mvc; 
    using System.DirectoryServices; 
    using System.DirectoryServices.AccountManagement; 
    using System.Web.Security; 
    using TMVCRepository.DataAccessLayer; 

     namespace TMVCRepository.Controllers 
    { 
     public class ActivedirectoryController : Controller 
     { 

      public ActionResult Index() 
      { 
       return View(); 
      } 





      [HttpPost] 

      public ActionResult index(ActivedirectoryModels UC) 
      { 


       if (ModelState.IsValid) 
       { 


        string userid = Request.Form["UserID"]; 
        string password = Request.Form["Password"]; 
        string domainname = Request.Form["DomainName"]; 


        return RedirectToAction("submit", 
           "Activedirectory", 
           new { userid = userid, password = password, 
     domainname = domainname }); 

       } 

       else 
       { 

        ModelState.AddModelError("", "Error in Viewing data"); 

        return View(); 

       } 

      } 
      public ActionResult submit(string userid, string password, string 
     domainname) 
      { 
       string group1 = null; 
       string group2 = null; 
       string group3 = null; 
       string group4 = null; 
       string group5 = null; 


       try 
       { 
        bool value = IsAuthenticated(domainname, userid, password); 


        if (value == true) 
        { 
         DBclass obj1 = new DBclass(); 
         string result = obj1.chkuserentry(userid); 
         if (result == "Yes") 
         { 
          FormsAuthentication.SetAuthCookie(userid, true); 

          // begin 


         using (var context1 = new PrincipalContext(ContextType.Domain, domainname)) 
          { 
           if(userid!="testadmin") 
           { 
           using (var exuser = UserPrincipal.FindByIdentity(context1, userid)) 
           { 
            var groups = exuser.GetGroups(); 

           string group_name = null; 
           foreach (object obj in groups) 
           { 
            group_name += "~" + obj.ToString(); 

           } 

           group_name = group_name.Replace("~Domain 
    Users",""); 


           //oadd split code 

           string[] strTemp = group_name.Split('~'); 


            for (int i = 0; i < strTemp.Length; i++) 
            { 

             if (i == 0) 
             { group1 = strTemp[0]; } 
             if (i == 1) 
             { group2 = strTemp[1]; } 
             if (i == 2) 
             { group3 = strTemp[2]; } 
             if (i == 3) 
             { group4 = strTemp[3]; } 
             if (i == 4) 
             { group5 = strTemp[4]; } 



            } 



            if (group1 == null || group1 == "") 
            { group1 = "N/A"; } 
            if (group2 == null || group2 == "") 
            { group2 = "N/A"; } 
            if (group3 == null || group3 == "") 
            { group3 = "N/A"; } 
            if (group4 == null || group4 == "") 
            { group4 = "N/A"; } 
            if (group5 == null || group5 == "") 
            { group5 = "N/A"; } 



           DBclass obj2 = new DBclass(); 


           string result1 = obj1.Exist_user(userid, group1, 
group2, group3, group4, group5);  



          } 
         } 

        } 

         //end 


         return RedirectToAction("Index", "Home"); 

        } 

        else 
        { 

         using (var context = new 
    PrincipalContext(ContextType.Domain, domainname)) 
         { 
          using (var user = 
    UserPrincipal.FindByIdentity(context, userid)) 
          { 

           var groups = user.GetGroups(); 
           string groupname = ""; 
           foreach (object obj in groups) 
           { 
            groupname += "~" + obj.ToString(); 

           } 



           TempData["userid"] = userid; 


           TempData["Password"] = password; 
           TempData["Group"] = groupname; 
           return RedirectToAction("Register", "Account"); 

          } 
         } 

        } 


       } 


       else 
       { 


        return View("test"); 
       } 








      } 

      catch (Exception ex) 
      { 


       return View("test"); 

      } 



     } 


     public bool IsAuthenticated(string srvr, string usr, string pwd) 
     { 

      bool authenticated = false; 

      try 
      { 

     DirectoryEntry entry = new DirectoryEntry("LDAP://" + srvr, usr + 
    "@" + srvr,pwd, AuthenticationTypes.Secure); 


      } 
      catch (DirectoryServicesCOMException cex) 
      { 

      } 


      catch (Exception ex) 
      { 
       //not authenticated due to some other exception [this is optional] 
      } 

      return authenticated; 
     } 
    } 
} 
+0

請包括您使用的確切代碼。就目前而言,不可能有用地回答你的問題。 –

+0

我有包括code.plz去通過它.. –

+0

有沒有一種方法來開發一個不同的應用程序來檢索窗口的用戶名稱,並通過該用戶名稱它到本應用程序..? –

回答

0

如果用戶沒有登錄到Web應用程序,你會怎麼考慮,以獲得他們的用戶名?

你不能。

它在開發過程中工作的事實是因爲您要求網絡服務器下運行哪個用戶,並且開發服務器在開發人員帳戶下運行。

+0

是否有出路開發一個不同的應用程序來檢索Windows用戶名並將該用戶名傳遞給本應用程序。 –

+0

嗯,也許如果你寫一個瀏覽器插件,但你想解決什麼問題,真的嗎?如果您使用Windows身份驗證,則瀏覽器將自動登錄您的用戶。 – CodeCaster

0

要在混合身份驗證模式下獲得自動身份驗證,請問您在問什麼? 然後我可以回答爲否,這是不可能的或者換句話說從來沒有實際做過

+0

是否有出路開發一個不同的應用程序來檢索Windows用戶名並將該用戶名傳遞給本應用程序。 –

+0

嘿,你不能將Windows密碼傳遞給另一個應用程序,你可以使用戶名以某種方式旅行 – DotNetXpert