Öncelikle uygulamamızı Azure üzerinde ekliyoruz.
Ardından bilgileri giriyoruz. Burada “Oturum Açma URL” bilgisi önemli. Login olduktan sonra uygulamamızda açılmasını istediğimiz sayfa bilgisi
Uygulamamızı Azure’a tanımladık. Bundan sonra kodlarımızı düzenleyeceğiz. Onun için de Microsoft’un örnek kodları ve örnek projeyi paylaştığı “ Uygulama kayıtları (Önizleme)” menüsüne gidip, hangi tipte proje yapacaksak seçiyoruz.
Açılan sayfadaki kodları uygulamamıza ekliyoruz. Yukarıda gördüğümüz id bilgilerini Web.config’e ekliyoruz ilk olarak.
<appSettings> <add key = "ClientId" value="f5697701-3d73-4b85-b905-0ca2a698b127" /> //Uygulama kimliği <add key = "Tenant" value="*****-099f-44ac-b918-2a8b72110b59" /> //Dizin kimliği <add key = "Authority" value="https://login.microsoftonline.com/{0}/v2.0" /> <add key = "redirectUri" value="http://localhost:59740/Login/" /> //Uygulamayı tanımlarken girdiğimiz Oturum Açma URL bilgisi </appSettings>
Sonra uygulamamıza Nuget’den aşağıdaki paketleri indiriyoruz. Yalnız bunların son versiyonlarını indirince çalıştıramadım. 4.0.0 versiyonlarını indirdiğimde düzgün çalıştı.
Install-Package Microsoft.Owin.Security.OpenIdConnect
Install-Package Microsoft.Owin.Security.Cookies
Install-Package Microsoft.Owin.Host.SystemWeb
Uygulamamızın ana dizine Startup.cs sınıfnı ekliyoruz ve aşağıdaki kodları yazıyoruz.
public class Startup { // The Client ID is used by the application to uniquely identify itself to Azure AD. string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"]; // RedirectUri is the URL where the user will be redirected to after they sign in. string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"]; // Tenant is the tenant ID (e.g. contoso.onmicrosoft.com, or 'common' for multi-tenant) static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"]; // Authority is the URL for authority, composed by Azure Active Directory v2 endpoint and the tenant name (e.g. https://login.microsoftonline.com/contoso.onmicrosoft.com/v2.0) string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, System.Configuration.ConfigurationManager.AppSettings["Authority"], tenant); /// <summary> /// Configure OWIN to use OpenIdConnect /// </summary> /// <param name="app"></param> public void Configuration(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { // Sets the ClientId, authority, RedirectUri as obtained from web.config ClientId = clientId, Authority = authority, RedirectUri = redirectUri, // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page PostLogoutRedirectUri = redirectUri, Scope = OpenIdConnectScope.OpenIdProfile, // ResponseType is set to request the id_token - which contains basic information about the signed-in user ResponseType = OpenIdConnectResponseType.IdToken, // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application // To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name // To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = false }, // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method Notifications = new OpenIdConnectAuthenticationNotifications { AuthenticationFailed = OnAuthenticationFailed } } ); } /// <summary> /// Handle failed authentication requests by redirecting the user to the home page with an error in the query string /// </summary> /// <param name="context"></param> /// <returns></returns> private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) { context.HandleResponse(); context.Response.Redirect("/?errormessage=" + context.Exception.Message); return Task.FromResult(0); } }
Login işlemini yöneteceğimiz sayfayı da aşağıdaki şekilde düzenleyince işlem tamamdır.