2017-07-19 39 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