JoeCool
JoeCool

Reputation: 4432

Why is ServiceSecurityContext.Current null in an MVC Controller?

I have an ASP.NET MVC 4 project that's using Windows authentication in the Web.config like so:

<system.web>
    <authentication mode="Windows" />
</system.web>

However, if I investigate ServiceSecurityContext.Current from a Controller method, it's null. Shouldn't it contain the authentication info of the user since I'm using Windows authentication?

The reason I'm trying to figure this out is because I would like to know what credentials CredentialCache.DefaultNetworkCredentials is using from a Controller method. From what I gathered by reading the MSDN article on the property, it uses the current security context's credentials... which is null.

Thanks for the help!

Upvotes: 1

Views: 2385

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

The ServiceContext classes is intended to be used inside WCF services. It has nothing to do with ASP.NET MVC.

Trying to use ServiceContext.Current inside an ASP.NET MVC application is like trying to use HttpContext.Current inside a console application => you get NULL.

The reason I'm trying to figure this out is because I would like to know what credentials CredentialCache.DefaultNetworkCredentials is using from a Controller method

Then you are looking for the User.Identity.Name property:

[Authorize]
public ActionResult Index()
{
    string currentUser = User.Identity.Name;
    ...
}

Upvotes: 5

Related Questions