簡體   English   中英

User.Identity.Name為空白

[英]User.Identity.Name is blank

我已經對此進行了大量研究,但是找不到解決該問題的方法。

我的web.config:

  <system.web>
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5.2"/>
    <authentication mode="Windows"/>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>

由於某種原因

string User = User.Identity.Name;

始終為空。

IIS Windows身份驗證是唯一啟用的選項。

我試過了

[Authorize]

同樣的結果。

我檢查了IIS配置文件以確保

  <windowsAuthentication enabled="true">

設置為true。

您是否在授權的環境中工作? 換句話說,除非您實際上說您要授權特定的控制器或操作,否則即使用戶先前已登錄也不會填充任何內容。例如:

public ActionResult Foo()
{
    var user = User.Identity.Name; // null
}

[Authorize]
public ActionResult Foo()
{
   var user = User.Identity.Name; // WIN
}

如果您需要采取措施仍允許匿名訪問,則只需添加[AllowAnonymous]

[Authorize]
[AllowAnonymous]
public ActionResult Foo()
{
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM